Java Program for Number of local extrema in an array Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article Like Report You are given an array on n-elements. An extrema is an elements which is either greater than its both of neighbors or less than its both neighbors. You have to calculate the number of local extrema in given array. Note : 1st and last elements are not extrema.Examples : Input : a[] = {1, 5, 2, 5} Output : 2 Input : a[] = {1, 2, 3} Output : 0 Approach :For calculating number of extrema we have to check whether an element is maxima or minima i.e. whether it is greater than both of its neighbors or less than both neighbors. For this simply iterate over the array and for each elements check its possibility of being an extrema.Note: a[0] and a[n-1] has exactly one neighbour each, they are neither minima nor maxima. Java // Java to find // number of extrema import java.io.*; class GFG { // function to find // local extremum static int extrema(int a[], int n) { int count = 0; // start loop from // position 1 till n-1 for (int i = 1; i < n - 1; i++) { // only one condition // will be true at a // time either a[i] // will be greater than // neighbours or less // than neighbours // check if a[i] is greater // than both its neighbours // then add 1 to x if(a[i] > a[i - 1] && a[i] > a[i + 1]) count += 1; // check if a[i] is // less than both its // neighbours, then // add 1 to x if(a[i] < a[i - 1] && a[i] < a[i + 1]) count += 1; } return count; } // driver program public static void main(String args[]) throws IOException { int a[] = { 1, 0, 2, 1 }; int n = a.length; System.out.println(extrema(a, n)); } } /* This code is contributed by Nikita Tiwari.*/ Output : 2 Time Complexity: O(n) where n is size of input array. This is because a for loop is executing from 1 to n. Space Complexity: O(1) as no extra space has been used. Please refer complete article on Number of local extrema in an array for more details! Comment More infoAdvertise with us Next Article Java Program for Number of local extrema in an array kartik Follow Improve Article Tags : Java Searching Java Programs DSA Arrays +1 More Practice Tags : ArraysJavaSearching Similar Reads Java Program for Equilibrium index of an array Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A: Example : Input: A[] = {-7, 1, 5, 2, -4, 3, 0} Output: 3 3 is an equilibrium index, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] 5 min read Java Program for Maximum equilibrium sum in an array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[]. Examples : Input : arr[] = {-1, 2, 3, 0, 3, 2, -1} Output : 4 Prefix sum of arr[0..3] = Suffix sum of arr[3..6] Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2} Output : 7 Prefix sum of arr[0..3] = Su 4 min read Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou 4 min read Java Program for Ceiling in a sorted array Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Assume than the array is sorted in non-decreasing order. Write efficient functions to find floor and ceiling of x. Examp 4 min read Java Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 4 min read Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele 2 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Java Program to Print All the Repeated Numbers with Frequency in an Array The frequency of an element in an array is the count of the occurrence of that particular element in the whole array. Given an array that may contain duplicates, print all repeated/duplicate elements and their frequencies. Below is the discussion of this program by two approaches: Using a counter ar 4 min read Determine the Upper Bound of a Two Dimensional Array in Java Multidimensional arrays in Java are common and can be termed as an array of arrays. Data in a two-dimensional array in Java is stored in 2D tabular form. A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array can be seen as an array of the one-dimensional a 3 min read Java Program for Number of pairs with maximum sum Write a java program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j.Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the pai 4 min read Like