Elements greater than the previous and next element in an Array | C ++ program to print elements greater than the previous and next element in an Array ; Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printElements ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printElements ( arr , n ) ; return 0 ; }