Java Program for Number of local extrema in an array Last Updated : 08 May, 2023 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Find max or min value in an array of primitives using Java A simple method is traverse the array. We initialize min and max as first elements and then compare every element (Starting from 2nd) with current min and max.Java// Java program to find min & max in int[] // array without asList() public class MinNMax { public static void main(String[] args) { 3 min read For-Each Loop in Java The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is commonly used when the exact index of an element is not required.Example: Using a for-each lo 8 min read Java Program to Count Inversions in an array | Set 1 (Using Merge Sort) Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers 5 min read Get first and last elements from ArrayList in Java Given an array list, find the first and last elements of it. Examples: Input : aList = {10, 30, 20, 14, 2} Output : First = 10, Last = 2 Input : aList = {10, 30, 40, 50, 60} Output : First = 10, Last = 60 The last element is at index, size - 1 and the first element is stored at index 0. If we know h 3 min read Finding minimum and maximum element of a Collection in Java A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. These are: Finding minimum and maximum element of a Collection can be easily done using the Co 6 min read Like