Given an array of N integers. The task is to print the elements from the array which are greater than their immediate previous and next elements.

**Examples** :

Input: arr[] = {2, 3, 1, 5, 4, 9, 8, 7, 5}Output: 3, 5, 9 In above given example 3 is greater than its left element 2 and right element 1. Similar logic is applied to other elements hence our final output is 3, 5, 9.Input: arr[] = {1, 2, 3, 2, 1}Output: 3

**Approach**: Since there is no element on the left of the first element(arr[0]) and there is no element to the right of last element(arr[N-1]) so these two elements will be excluded from the final answer.

Now, traverse the array starting from the index **1 to N-2** and for every element **arr[i]** check if **arr[i] > arr[i-1]** and **arr[i] > arr[i+1]**.

Below is the implementation of the above approach:

## C++

`// C++ program to print elements greater than ` `// the previous and next element in an Array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print elements greater than ` `// the previous and next element in an Array ` `void` `printElements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Traverse array from index 1 to n-2 ` ` ` `// and check for the given condition ` ` ` `for` `(` `int` `i = 1; i < n - 1; i++) { ` ` ` `if` `(arr[i] > arr[i - 1] and arr[i] > arr[i + 1]) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print elements greater than ` `// the previous and next element in an Array ` `class` `GfG ` `{ ` ` ` `// Function to print elements greater than ` `// the previous and next element in an Array ` `static` `void` `printElements(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Traverse array from index 1 to n-2 ` ` ` `// and check for the given condition ` ` ` `for` `(` `int` `i = ` `1` `; i < n - ` `1` `; i++) ` ` ` `{ ` ` ` `if` `(arr[i] > arr[i - ` `1` `] && arr[i] > arr[i + ` `1` `]) ` ` ` `System.out.print(arr[i] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `3` `, ` `1` `, ` `5` `, ` `4` `, ` `9` `, ` `8` `, ` `7` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `printElements(arr, n); ` `} ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 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 ` `def` `printElements(arr, n): ` ` ` ` ` `# Traverse array from index 1 to n-2 ` ` ` `# and check for the given condition ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `-` `1` `, ` `1` `): ` ` ` `if` `(arr[i] > arr[i ` `-` `1` `] ` `and` ` ` `arr[i] > arr[i ` `+` `1` `]): ` ` ` `print` `(arr[i], end ` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `2` `, ` `3` `, ` `1` `, ` `5` `, ` `4` `, ` `9` `, ` `8` `, ` `7` `, ` `5` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` ` ` `printElements(arr, n) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `// c# program to print elements greater than ` `// the previous and next element in an Array ` `public` `class` `GfG ` `{ ` ` ` `// Function to print elements greater than ` `// the previous and next element in an Array ` `public` `static` `void` `printElements(` `int` `[] arr, ` `int` `n) ` `{ ` ` ` `// Traverse array from index 1 to n-2 ` ` ` `// and check for the given condition ` ` ` `for` `(` `int` `i = 1; i < n - 1; i++) ` ` ` `{ ` ` ` `if` `(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) ` ` ` `{ ` ` ` `Console.Write(arr[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[] {2, 3, 1, 5, 4, 9, 8, 7, 5}; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `printElements(arr, n); ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `printElements(` `$arr` `, ` `$n` `) ` `{ ` ` ` `// Traverse array from index 1 to n-2 ` ` ` `// and check for the given condition ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `- 1; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$arr` `[` `$i` `] > ` `$arr` `[` `$i` `- 1] ` `and` ` ` `$arr` `[` `$i` `] > ` `$arr` `[` `$i` `+ 1]) ` ` ` `echo` `$arr` `[` `$i` `] . ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(2, 3, 1, 5, 4, 9, 8, 7, 5); ` `$n` `= sizeof(` `$arr` `); ` ` ` `printElements(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

3 5 9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Rearrange an array such that every odd indexed element is greater than it previous
- Previous greater element
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of greater elements for each element in the Array
- Count of elements in first Array greater than second Array with each element considered only once
- Sum of elements in an array with frequencies greater than or equal to that element
- Adding elements of an array until every element becomes greater than or equal to k
- Count the number of elements which are greater than any of element on right side of an array
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Range Query on array whose each element is XOR of index value and previous element
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element of the array by its previous element
- Replace every array element by sum of previous and next
- Generate an array having Bitwise AND of the previous and the next element
- Replace every array element by multiplication of previous and next
- Find just strictly greater element from first array for each element in second array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Maximum element in an array such that its previous and next element product is maximum
- Count elements whose sum with K is greater than max element
- Find the element before which all the elements are smaller than it, and after which all are greater

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.