- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find all elements in array which have at-least two greater elements in C++

Suppose, we have an array of n numbers. We have to find all elements in array, which have at least two greater elements. If the array is like A = [2, 8, 7, 1, 5], then the result will be [2, 1, 5]

To solve this, we will find second max element, then print all elements which is less than or equal to second max value.

## Example

#include<iostream> using namespace std; void searchElements(int arr[], int n) { int first_max = INT_MIN, second_max = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] > first_max) { second_max = first_max; first_max = arr[i]; } else if (arr[i] > second_max) second_max = arr[i]; } for (int i = 0; i < n; i++) if (arr[i] < second_max) cout << arr[i] << " "; } int main() { int arr[] = { 2, 9, 1, 7, 5, 3, 17}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Elements are: "; searchElements(arr, n); }

## Output

Elements are: 2 1 7 5 3

- Related Questions & Answers
- Program to find elements from list which have occurred at least k times in Python
- Count all elements in the array which appears at least K times after their first occurrence in C++
- Subarray sum with at least two elements in JavaScript
- Program to find k where k elements have value at least k in Python
- Print array elements that are divisible by at-least one other in C++
- Find minimum value to assign all array elements so that array product becomes greater in C++
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Maximum sum subsequence with at-least k distant elements in C++
- Count elements that are divisible by at-least one element in another array in C++
- Is there a DOM function which deletes all elements between two elements in JavaScript?
- Count subarrays with all elements greater than K in C++
- Find documents where all elements of an array have a specific value in MongoDB?
- Maximum sum subsequence with at-least k distant elements in C++ program
- C# program to check if two lists have at-least one element common
- Program to find number of elements in A are strictly less than at least k elements in B in Python

Advertisements