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

Given an array of n distinct elements, the task is to find all elements in array which have at-least two greater elements than themselves.

Examples :

```Input : arr[] = {2, 8, 7, 1, 5};
Output : 2  1  5
The output three elements have two or
more greater elements

Input  : arr[] = {7, -2, 3, 4, 9, -1};
Output : -2  3  4 -1
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1 (Simple)
The naive approach is to run two loops and check one by one element of array check that array elements have at-least two elements greater than itself or not. If its true then print array element.

## C++

 `// Simple C++ program to find ` `// all elements in array which  ` `// have at-least two greater  ` `// elements itself. ` `#include ` `using` `namespace` `std; ` ` `  `void` `findElements(``int` `arr[], ``int` `n) ` `{ ` `    ``// Pick elements one by one and  ` `    ``// count greater elements. If  ` `    ``// count is more than 2, print  ` `    ``// that element. ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``int` `count = 0; ` `        ``for` `(``int` `j = 0; j < n; j++) ` `            ``if` `(arr[j] > arr[i]) ` `                ``count++; ` ` `  `        ``if` `(count >= 2) ` `            ``cout << arr[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, -6 ,3 , 5, 1}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``findElements(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find all  ` `// elements in array which  ` `// have at-least two greater ` `// elements itself. ` `import` `java.util.*; ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `static` `void` `findElements(``int` `arr[],  ` `                            ``int` `n) ` `{ ` `    ``// Pick elements one by one  ` `    ``// and count greater elements.  ` `    ``// If count is more than 2,  ` `    ``// print that element. ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``int` `count = ``0``; ` `         `  `        ``for` `(``int` `j = ``0``; j < n; j++) ` `            ``if` `(arr[j] > arr[i]) ` `                ``count++; ` ` `  `        ``if` `(count >= ``2``) ` `        ``System.out.print(arr[i] + ``" "``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``2``, -``6` `,``3` `, ``5``, ``1``}; ` `    ``int` `n = arr.length; ` `    ``findElements(arr, n); ` `} ` `} ` ` `  `// This code is contributed by Sahil_Bansall `

## Python3

 `# Python3 program to find ` `# all elements in array ` `# which have at-least two ` `# greater elements itself. ` ` `  `def` `findElements( arr, n): ` ` `  `    ``# Pick elements one by ` `        ``# one and count greater ` `    ``# elements. If count ` `        ``# is more than 2, print ` `    ``# that element. ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``count ``=` `0` `        ``for` `j ``in` `range``(``0``, n): ` `            ``if` `arr[j] > arr[i]: ` `                ``count ``=` `count ``+` `1` `                 `  `                 `  `                 `  `        ``if` `count >``=` `2` `: ` `            ``print``(arr[i], end``=``" "``) ` `             `  ` `  `# Driver code ` `arr ``=` `[ ``2``, ``-``6` `,``3` `, ``5``, ``1``] ` `n ``=` `len``(arr) ` `findElements(arr, n) ` `     `  `# This code is contributed by sunnysingh `

## C#

 `// C# program to find all elements in ` `// array which have at least two greater ` `// elements itself. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `static` `void` `findElements(``int` `[]arr, ``int` `n) ` `{ ` `    ``// Pick elements one by one and count  ` `    ``// greater elements. If count is more  ` `    ``// than 2, print that element. ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``int` `count = 0; ` `         `  `        ``for` `(``int` `j = 0; j < n; j++) ` `            ``if` `(arr[j] > arr[i]) ` `                ``count++; ` ` `  `        ``if` `(count >= 2) ` `    ``Console.Write(arr[i] + ``" "``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = {2, -6 ,3 , 5, 1}; ` `    ``int` `n = arr.Length; ` `    ``findElements(arr, n); ` ` `  `} ` `} ` ` `  `// This code is contributed by Parashar. `

## PHP

 ` ``\$arr``[``\$i``]) ` `                ``\$count``++; ` ` `  `        ``if` `(``\$count` `>= 2) ` `            ``echo` `\$arr``[``\$i``].``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `\$arr` `= ``array``( 2, -6 ,3 , 5, 1); ` `\$n` `= sizeof(``\$arr``); ` `findElements(``\$arr``, ``\$n``); ` ` `  `?> `

Output :

```2  -6  1
```

Time Complexity :
O(n2)

Method 2 (Use Sorting)
We sort the array first in increasing order, then we print first n-2 elements where n is size of array.

## C++

 `// Sorting based C++ program to  ` `// find all elements in array  ` `// which have atleast two greater  ` `// elements itself. ` `#include ` `using` `namespace` `std; ` ` `  `void` `findElements(``int` `arr[], ``int` `n) ` `{ ` `    ``sort(arr, arr + n); ` ` `  `    ``for` `(``int` `i = 0; i < n - 2; i++) ` `    ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, -6 ,3 , 5, 1}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``findElements(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Sorting based Java program to find  ` `// all elements in array which have  ` `// atleast two greater elements itself. ` `import` `java.util.*; ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` ` `  `static` `void` `findElements(``int` `arr[], ``int` `n) ` `{ ` `    ``Arrays.sort(arr); ` ` `  `    ``for` `(``int` `i = ``0``; i < n - ``2``; i++) ` `    ``System.out.print(arr[i] + ``" "``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``2``, -``6` `,``3` `, ``5``, ``1``}; ` `    ``int` `n = arr.length; ` `    ``findElements(arr, n); ` ` `  `} ` `} ` ` `  `// This code is contributed by Sahil_Bansall `

## Python3

 `# Sorting based Python 3 program ` `# to find all elements in array  ` `# which have atleast two greater  ` `# elements itself. ` ` `  `def` `findElements(arr, n): ` ` `  `    ``arr.sort() ` ` `  `    ``for` `i ``in` `range``(``0``, n``-``2``): ` `        ``print``(arr[i], end ``=``" "``) ` ` `  `# Driven source ` `arr ``=` `[``2``, ``-``6``, ``3``, ``5``, ``1``] ` `n ``=` `len``(arr) ` `findElements(arr, n) ` ` `  `# This code is contributed  ` `# by Smitha Dinesh Semwal `

Output :

`-6 1 2`

## C#

 `// Sorting based C# program to find  ` `// all elements in array which have  ` `// atleast two greater elements itself. ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `static` `void` `findElements(``int` `[]arr, ``int` `n) ` `{ ` `    ``Array.Sort(arr); ` ` `  `    ``for` `(``int` `i = 0; i < n-2; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = { 2, -6 ,3 , 5, 1}; ` `    ``int` `n = arr.Length; ` `    ``findElements(arr, n); ` ` `  `} ` `} ` ` `  `// This code is contributed by parashar `

## PHP

 `; `

Output :

`-6 1 2`

Time Complexity : O(n Log n)

Method 3 (Efficient)
In second method we simply calculate second maximum element of array and print all element which is less than or equal to second maximum.

## C++

 `// C++ program to find all elements ` `// in array which have atleast two  ` `// greater elements itself. ` `#include ` `using` `namespace` `std; ` ` `  `void` `findElements(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `first = INT_MIN,  ` `        ``second = INT_MIN; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``/* If current element is smaller  ` `        ``than first then update both first  ` `        ``and second */` `        ``if` `(arr[i] > first) ` `        ``{ ` `            ``second = first; ` `            ``first = arr[i]; ` `        ``} ` ` `  `        ``/* If arr[i] is in between first  ` `        ``and second then update second */` `        ``else` `if` `(arr[i] > second) ` `            ``second = arr[i]; ` `    ``} ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``if` `(arr[i] < second) ` `            ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, -6, 3, 5, 1}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``findElements(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find all elements ` `// in array which have atleast ` `// two greater elements itself. ` `import` `java.util.*; ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `static` `void` `findElements(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `first = Integer.MIN_VALUE; ` `    ``int` `second = Integer.MAX_VALUE; ` `     `  `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``// If current element is smaller  ` `        ``// than first then update both ` `        ``// first and second  ` `        ``if` `(arr[i] > first) ` `        ``{ ` `            ``second = first; ` `            ``first = arr[i]; ` `        ``} ` ` `  `        ``/* If arr[i] is in between  ` `        ``first and second ` `        ``then update second */` `        ``else` `if` `(arr[i] > second) ` `            ``second = arr[i]; ` `    ``} ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``if` `(arr[i] < second) ` `            ``System.out.print(arr[i] + ``" "``) ; ` `} ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``2``, -``6``, ``3``, ``5``, ``1``}; ` `    ``int` `n = arr.length; ` `    ``findElements(arr, n); ` `} ` `} ` ` `  `// This code is contributed by Sahil_Bansall `

## Python3

 `# Python 3 program to find all elements ` `# in array which have atleast two  ` `# greater elements itself. ` `import` `sys ` ` `  `def` `findElements(arr, n): ` ` `  `    ``first ``=` `-``sys.maxsize ` `    ``second ``=` `-``sys.maxsize ` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `     `  `        ``# If current element is smaller ` `        ``# than first then update both ` `        ``# first and second  ` `        ``if` `(arr[i] > first): ` `         `  `            ``second ``=` `first ` `            ``first ``=` `arr[i] ` `         `  `        ``# If arr[i] is in between first ` `        ``# and second then update second  ` `        ``elif` `(arr[i] > second): ` `            ``second ``=` `arr[i] ` `     `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``if` `(arr[i] < second): ` `            ``print``(arr[i], end ``=``" "``) ` ` `  ` `  `# Driver code ` `arr ``=` `[``2``, ``-``6``, ``3``, ``5``, ``1``] ` `n ``=` `len``(arr) ` `findElements(arr, n) ` ` `  `# This code is contributed ` `# by Smitha Dinesh Semwal `

## C#

 `// C# program to find all elements ` `// in array which have atleast ` `// two greater elements itself. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `void` `findElements(``int` `[]arr,  ` `                            ``int` `n) ` `    ``{ ` `    ``int` `first = ``int``.MinValue; ` `    ``int` `second = ``int``.MaxValue; ` `     `  `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``// If current element is smaller  ` `        ``// than first then update both  ` `        ``// first and second  ` `        ``if` `(arr[i] > first) ` `        ``{ ` `            ``second = first; ` `            ``first = arr[i]; ` `        ``} ` ` `  `        ``/* If arr[i] is in between  ` `        ``first and second ` `        ``then update second */` `        ``else` `if` `(arr[i] > second) ` `            ``second = arr[i]; ` `    ``} ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``if` `(arr[i] < second) ` `            ``Console.Write(arr[i] + ``" "``) ; ` `} ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr = { 2, -6, 3, 5, 1}; ` `    ``int` `n = arr.Length; ` `    ``findElements(arr, n); ` `} ` `} ` ` `  `// This code is contributed by parashar... `

## PHP

 ` ``\$first``) ` `            ``{ ` `                ``\$second` `= ``\$first``; ` `                ``\$first` `= ``\$arr``[``\$i``]; ` `             `  `     `  `            ``} ` ` `  `        ``/* If arr[i] is in between first  ` `           ``and second then update second */` `        ``else` `if` `(``\$arr``[``\$i``] > ``\$second``) ` `            ``\$second` `= ``\$arr``[``\$i``]; ` `    ``} ` ` `  `    ``for``(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``if` `(``\$arr``[``\$i``] < ``\$second``) ` `            ``echo` `\$arr``[``\$i``] , ``" "``; ` `} ` ` `  `    ``// Driver code ` `    ``\$arr` `= ``array``(2, -6, 3, 5, 1); ` `    ``\$n` `= ``count``(``\$arr``); ` `    ``findElements(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by vishal tripathi. ` `?> `

Output :

```2  -6  1
```

Time Complexity : O(n)

This article is contributed by DANISH_RAZA . 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.

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.

My Personal Notes arrow_drop_up

Improved By : parashar, jit_t, vt_m

Article Tags :
Practice Tags :

15

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.