Number of local extrema in an array | function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] if greater than both its neighbours , then add 1 to x ; check if a [ i ] if less than both its neighbours , then add 1 to x ; driver program
def extrema ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT count += ( a [ i ] > a [ i - 1 ] and a [ i ] > a [ i + 1 ] ) ; NEW_LINE count += ( a [ i ] < a [ i - 1 ] and a [ i ] < a [ i + 1 ] ) ; NEW_LINE DEDENT return count NEW_LINE DEDENT a = [ 1 , 0 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( extrema ( a , n ) ) NEW_LINE