Find indices of all local maxima and local minima in an Array | Function to find all the local maxima and minima in the given array arr [ ] ; Empty lists to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or neither ; 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 neither ; Print all the local maxima and local minima indexes stored ; Driver Code ; Given array arr [ ] ; Function Call
def findLocalMaximaMinima ( n , arr ) : NEW_LINE INDENT mx = [ ] NEW_LINE mn = [ ] NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT mx . append ( 0 ) NEW_LINE DEDENT elif ( arr [ 0 ] < arr [ 1 ] ) : NEW_LINE INDENT mn . append ( 0 ) NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] > arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT mn . append ( i ) NEW_LINE DEDENT elif ( arr [ i - 1 ] < arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT mx . append ( i ) NEW_LINE DEDENT DEDENT if ( arr [ - 1 ] > arr [ - 2 ] ) : NEW_LINE INDENT mx . append ( n - 1 ) NEW_LINE DEDENT elif ( arr [ - 1 ] < arr [ - 2 ] ) : NEW_LINE INDENT mn . append ( n - 1 ) NEW_LINE DEDENT if ( len ( mx ) > 0 ) : NEW_LINE INDENT print ( " Points ▁ of ▁ Local ▁ maxima " \ " ▁ are ▁ : ▁ " , end = ' ' ) NEW_LINE print ( * mx ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ points ▁ of " \ " ▁ Local ▁ maxima . " ) NEW_LINE DEDENT if ( len ( mn ) > 0 ) : NEW_LINE INDENT print ( " Points ▁ of ▁ Local ▁ minima " \ " ▁ are ▁ : ▁ " , end = ' ' ) NEW_LINE print ( * mn ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ points " \ " ▁ of ▁ Local ▁ minima . " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE arr = [ 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 ] NEW_LINE findLocalMaximaMinima ( N , arr ) NEW_LINE DEDENT