Traverse from the second element to the second last element
for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( p [ i - 1 ] > p [ i ] and p [ i ] > p [ i + 1 ] ) ans ++ ; else if ( p [ i - 1 ] < p [ i ] and p [ i ] < p [ i + 1 ] ) ans ++ ; }