Count of elements which are second smallest among three consecutive elements | Function to return the count of elements P [ i ] such that P [ i ] is the second smallest among P [ i 1 ] , P [ i ] and P [ i + 1 ] ; To store the required answer ; Traverse from the second element to the second last element ; Return the required answer ; Driver code
def countElements ( p , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( p [ i - 1 ] > p [ i ] and p [ i ] > p [ i + 1 ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT elif ( p [ i - 1 ] < p [ i ] and p [ i ] < p [ i + 1 ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ 2 , 5 , 1 , 3 , 4 ] ; NEW_LINE n = len ( p ) ; NEW_LINE print ( countElements ( p , n ) ) ; NEW_LINE DEDENT