
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Elements greater than the previous and next element in an Array in C++
In this problem, we are given an array arr[] of n positive integers. Our task is to create a program to find the elements greater than the previous and next element in an Array.
Code Description: We need to find the elements of the array that satisfy the condition, the element is greater that the element at index 1 less than it and also is greater than the element at index 1 greater than it.
Let’s take an example to understand the problem,
Input: arr[] = {3, 2, 5, 7, 3, 4, 5}
Output: 7
Explanation −
Element with index one less than the current element, 5.
Element with index one more than the current element, 3.
The current element is greater than both.
Solution Approach −
A simple solution to the problem is by checking the condition for each element of the array and then print the element that satisfies the condition.
For this we need to follow these steps−
Step 1: Loop through the elements of the array from index 1 to n-2.
Step 1.1: for each element, arr[i]. We will check if arr[i] > arr[i-1] and arr[i] > arr[i+1].
Step 1.2: If it is true, print arr[i].
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; void findElemenetsInArray(int arr[], int n) { for (int i = 1; i < n-1; i++) if ( (arr[i] > arr[i+1] && arr[i] > arr[i-1]) ) { cout<<arr[i]<<"\t"; } } int main() { int n = 8; int arr[n] = { 5, 4, 7, 1, 17, 8, 3 }; cout<<"The elements that satisfy the given condition are "; findElemenetsInArray(arr, n); return 0; }
Output −
The elements that satisfy the given condition are 7 17
- Related Articles
- Rearrange an array such that every odd indexed element is greater than its previous in C++
- Python – Sort Matrix by Number of elements greater than its previous element
- Next Greater Element in Circular Array in JavaScript
- Previous greater element in C++
- Find next Smaller of next Greater in an array in C++
- Next Greater Element in C++
- Find Array Elements Which are Greater Than Its Immediate Left Element?
- Adding elements of an array until every element becomes greater than or equal to k in C++.
- Finding element greater than its adjacent elements in JavaScript
- Next Greater Element II in C++
- Next Greater Element III in C++
- Return the truth value of an array greater than another element-wise in Numpy
- Find Array Elements Which are Greater than its Left Elements in Java?
- Mask array elements greater than a given value in Numpy
- C++ program to Adding elements of an array until every element becomes greater than or equal to k
