Given an array of distinct elements, print the closest greater element for every element. The closest greater element for an element x is the smallest element on the right side of x in array which is greater than x. Elements for which no greater element exist, consider next greater element as -1.

**Examples: **

Input: arr[] = {4, 5, 2, 25} Output: Element NGE 4 --> 5 5 --> 25 2 --> 25 25 --> -1 Input: arr[] = {4, 10, 7} Output: Element NGE 4 --> 7 10 --> -1 7 --> -1

**Approach:** In this post, we will be discussing how to find the Next Greater Element using C++ STL(set).

Finding the smallest greater element on the right side will be like finding the first greater element of the current element in a list that is sorted.

Consider example 1, The sorted list would look like 2, 4, 5, 25.

Here for element 4, the greater element is 5 as it is next to it, so we print 5 and remove 4 because it would not be greater to the other elements since it is no longer on anyone’s right.

Similarly, for 5 it is 25 and we remove 5 from the list, as 5 will not be on the right side of 2 or 25, so it can be deleted.

Given below are the steps to find the Next Greater Element of every index element.

- Insert all the elements in a Set, it will store all the elements in an increasing order.
- Iterate on the array of elements, and for each index, find the upper_bound of the current index element. The upper_bound() returns an iterator which can point to the following position.
- If the iterator is pointing to a position past the last element, then there exists no NGE to the current index element.
- If the iterator points to a position referring to an element, then that element is the NGE to the current index element.

- Find the position of current index element at every traversal and remove it from the set using >lower_bound() and erase() functions of set.

Below is the implementation of the above approach.

## C/C++

`// C++ program to print the ` `// NGE's of array elements using ` `// C++ STL ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the NGE ` `void` `printNGE(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `set<` `int` `> ms; ` ` ` ` ` `// insert in the multiset container ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ms.insert(a[i]); ` ` ` ` ` `cout << ` `"Element "` ` ` `<< ` `"NGE"` `; ` ` ` ` ` `// traverse for all array elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// find the upper_bound in set ` ` ` `auto` `it = ms.upper_bound(a[i]); ` ` ` ` ` `// if points to the end, then ` ` ` `// no NGE of that element ` ` ` `if` `(it == ms.end()) { ` ` ` `cout << ` `"\n "` `<< a[i] ` ` ` `<< ` `" ----> "` `<< -1; ` ` ` `} ` ` ` ` ` `// print the element at that position ` ` ` `else` `{ ` ` ` `cout << ` `"\n "` `<< a[i] ` ` ` `<< ` `" ----> "` `<< *it; ` ` ` `} ` ` ` ` ` `// find the first occurrence of ` ` ` `// the index element and delete it ` ` ` `it = ms.lower_bound(a[i]); ` ` ` ` ` `// delete one occurrence ` ` ` `// from the container ` ` ` `ms.erase(it); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 4, 5, 2, 25 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// Function call to print the NGE ` ` ` `printNGE(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// C++ program to print the ` `// NGE's of array elements using ` `import` `java.util.TreeSet; ` ` ` `class` `Geeks { ` ` ` ` ` `// Function to print the NGE ` ` ` `static` `void` `printNGE(` `int` `[] a, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Tree Set is an ordered set used to ` ` ` `// store elements in a sorted manner ` ` ` `TreeSet<Integer> t = ` `new` `TreeSet<>(); ` ` ` ` ` `// Adding elements into the set ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `t.add(a[i]); ` ` ` ` ` `System.out.println(` `"ELEMENT NGE"` `); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// If the elements does not have an upper bound ` ` ` `// or an element greater than it, ` ` ` `// higher method of TreeSet class will return NULL ` ` ` `if` `(t.higher(a[i]) == ` `null` `) ` ` ` `System.out.println(a[i] + ` `" ----> "` ` ` `+ ` `"-1"` `); ` ` ` ` ` `// Otherwise print the upper bound of that element ` ` ` `else` ` ` `System.out.println(a[i] + ` `" ----> "` `+ t.higher(a[i])); ` ` ` ` ` `// Remove the current element from the set ` ` ` `t.remove(a[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `a[] = { ` `4` `, ` `5` `, ` `2` `, ` `25` `}; ` ` ` `int` `n = a.length; ` ` ` ` ` `printNGE(a, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print the ` `# NGE's of array elements ` `from` `bisect ` `import` `bisect_right as upper_bound, \ ` ` ` `bisect_left as lower_bound ` ` ` `# Function to print the NGE ` `def` `printNGE(a: ` `list` `, n): ` ` ` `ms ` `=` `set` `() ` ` ` ` ` `# insert in the multiset container ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `ms.add(a[i]) ` ` ` ` ` `print` `(` `"Element NGE"` `, end ` `=` `"") ` ` ` ` ` `# Required because Python sets ` ` ` `# are not sorted ` ` ` `new_arr ` `=` `list` `(ms) ` ` ` `new_arr.sort() ` ` ` ` ` `# traverse for all array elements ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# find the upper_bound in set ` ` ` `it ` `=` `upper_bound(new_arr, a[i]) ` ` ` ` ` `# if points to the end, then ` ` ` `# no NGE of that element ` ` ` `if` `(it ` `=` `=` `len` `(new_arr)): ` ` ` `print` `(` `"\n %d ----> -1"` `%` `a[i], end ` `=` `"") ` ` ` ` ` `# print the element at that position ` ` ` `else` `: ` ` ` `print` `(` `"\n %d ----> %d"` `%` `(a[i], ` ` ` `new_arr[it]), end ` `=` `"") ` ` ` ` ` `# find the first occurrence of ` ` ` `# the index element and delete it ` ` ` `it ` `=` `lower_bound(new_arr, a[i]) ` ` ` ` ` `# delete one occurrence ` ` ` `# from the container ` ` ` `new_arr.remove(new_arr[it]) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `a ` `=` `[` `4` `, ` `5` `, ` `2` `, ` `25` `] ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `# Function call to print the NGE ` ` ` `printNGE(a, n) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

Element NGE 4 ----> 5 5 ----> 25 2 ----> 25 25 ----> -1

**Time Complexity:** O(N log N)

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:

- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count the number of elements which are greater than any of element on right side of an array
- Replace every element with the greatest element on right side
- Replace every element with the smallest element on its left side
- Replace every element with the least greater element on its right
- Ceiling in right side for every element in an array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Replace every element of array with sum of elements on its right side
- Closest greater or same value on left side for every element in array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Smallest subarray of size greater than K with sum greater than a given value
- Replace every element with the greatest element on its left side
- Count smaller elements on right side
- Steps to make array empty by removing maximum and its right side
- Count smaller elements on right side using Set in C++ STL
- Number of elements that can be seen from right side
- Find the farthest smaller number in the right side
- Smallest element greater than X not present in the array
- Smallest subarray having an element with frequency greater than that of other elements
- Closest greater element for every array element from another array

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.