Elements greater than the previous and next element in an Array - GeeksforGeeks

08 Mar, 2022
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++
Java
Python3
C#
PHP
Javascript
// 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 Arrayvoid 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 Codeint main(){    int arr[] = { 2, 3, 1, 5, 4, 9, 8, 7, 5 };    int n = sizeof(arr) / sizeof(arr[0]);     printElements(arr, n);     return 0;}
// Java program to print elements greater than// the previous and next element in an Arrayclass GfG{ // Function to print elements greater than// the previous and next element in an Arraystatic 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 Codepublic 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
# 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 Arraydef 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 Codeif __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
using System; // c# program to print elements greater than// the previous and next element in an Arraypublic class GfG{ // Function to print elements greater than// the previous and next element in an Arraypublic 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 Codepublic 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);}}
<?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 Arrayfunction 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
<script> // Javascript implementation of the approach // Function to print elements greater than// the previous and next element in an Arrayfunction printElements(arr,  n){    // Traverse array from index 1 to n-2    // and check for the given condition    for (var i = 1; i < n - 1; i++) {        if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])            document.write( arr[i] + " ");    }}var arr = [ 2, 3, 1, 5, 4, 9, 8, 7, 5 ];var n = arr.length; printElements(arr, n); // This code is contributed by SoumikMondal </script>
3 5 9
 
Time Complexity : O(n)Auxiliary Space : O(1)
prerna saini
shrikanth13
SURENDRA_GANGWAR
Akanksha_Rai
SoumikMondal
singhh3010
school-programming
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Multidimensional Arrays in Java
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7