Find indices of all local maxima and local minima in an Array | Java program for the above approach ; Function to find all the local maxima and minima in the given array arr [ ] ; Empty vector to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or none ; Iterating over all points to check local maxima and local minima ; Condition for local minima ; Condition for local maxima ; Checking whether the last point is local maxima or minima or none ; Print all the local maxima and local minima indexes stored ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { public static void findLocalMaximaMinima ( int n , int [ ] arr ) { Vector < Integer > mx = new Vector < Integer > ( ) ; Vector < Integer > mn = new Vector < Integer > ( ) ; if ( arr [ 0 ] > arr [ 1 ] ) mx . add ( 0 ) ; else if ( arr [ 0 ] < arr [ 1 ] ) mn . add ( 0 ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] > arr [ i ] ) && ( arr [ i ] < arr [ i + 1 ] ) ) mn . add ( i ) ; else if ( ( arr [ i - 1 ] < arr [ i ] ) && ( arr [ i ] > arr [ i + 1 ] ) ) mx . add ( i ) ; } if ( arr [ n - 1 ] > arr [ n - 2 ] ) mx . add ( n - 1 ) ; else if ( arr [ n - 1 ] < arr [ n - 2 ] ) mn . add ( n - 1 ) ; if ( mx . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " maxima ▁ are ▁ : ▁ " ) ; for ( Integer a : mx ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ " + " of ▁ Local ▁ Maxima ▁ " ) ; if ( mn . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " minima ▁ are ▁ : ▁ " ) ; for ( Integer a : mn ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ of ▁ " + " Local ▁ Maxima ▁ " ) ; } public static void main ( String [ ] args ) { int N = 9 ; int arr [ ] = { 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 } ; findLocalMaximaMinima ( N , arr ) ; } }