Given an array of integers, find the closest greater element for every element. If there is no greater element then print -1

**Examples:**

Input : arr[] = {10, 5, 11, 6, 20, 12}

Output : 11 6 12 10 -1 20Input : arr[] = {10, 5, 11, 10, 20, 12}

Output : 11 10 12 11 -1 20

A **simple solution** is to run two nested loops. We pick an outer element one by one. For every picked element, we traverse remaining array and find closest greater element. Time complexity of this solution is O(n*n)

A **better solution** is to use sorting. We sort all elements, then for every element, traverse toward right until we find a greater element (Note that there can be multiple occurrences of an element).

An **efficient solution **is to use Self Balancing BST (Implemented as set in C++ and TreeSet in Java). In a Self Balancing BST, we can do both insert and closest greater operations in O(Log n) time.

## C++

`// C++ program to find closest greater value ` `// for every array element ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `closetGreater(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Insert all array elements into a Set ` ` ` `set<` `int` `> ts; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ts.insert(arr[i]); ` ` ` ` ` `// Find smallest greater element for ` ` ` `// every array element ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `auto` `greater = ts.upper_bound(arr[i]); ` ` ` ` ` `if` `(greater == ts.end()) ` ` ` `cout << -1 << ` `" "` `; ` ` ` `else` ` ` `cout << *greater << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main(` `int` `argc, ` `char` `const` `*argv[]) ` `{ ` ` ` `int` `arr[] = {10, 5, 11, 10, 20, 12}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `closetGreater(arr, n); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find closest greater value for ` `// every array element ` `import` `java.util.*; ` ` ` `class` `TreeSetDemo { ` ` ` `public` `static` `void` `closestGreater(` `int` `[] arr) ` ` ` `{ ` ` ` `// Insert all array elements into a TreeSet ` ` ` `TreeSet<Integer> ts = ` `new` `TreeSet<Integer>(); ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) ` ` ` `ts.add(arr[i]); ` ` ` ` ` `// Find smallest greater element for every ` ` ` `// array element ` ` ` `for` `(` `int` `i = ` `0` `; i < arr.length; i++) { ` ` ` `Integer greater = ts.higher(arr[i]); ` ` ` `if` `(greater == ` `null` `) ` ` ` `System.out.print(-` `1` `+ ` `" "` `); ` ` ` `else` ` ` `System.out.print(greater + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `10` `, ` `5` `, ` `11` `, ` `10` `, ` `20` `, ` `12` `}; ` ` ` `closestGreater(arr); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

11 10 12 11 -1 20

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:

- Closest greater element for every array element from another array
- Closest greater or same value on left side for every element in array
- Find closest smaller value for every element in array
- Find closest value for every element in array
- Distance of closest zero to every element
- Replace every element with the least greater element on its right
- Find just strictly greater element from first array for each element in second array
- Largest element smaller than current element on left for every element in Array
- Rearrange an array such that every odd indexed element is greater than it previous
- Adding elements of an array until every element becomes greater than or equal to k
- Find the element that appears once in an array where every other element appears twice
- Find last element after deleting every second element in array of n integers
- Smallest subarray of size greater than K with sum greater than a given value
- Find k closest elements to a given value
- Find the closest element in Binary Search Tree
- Find the closest element in Binary Search Tree | Space Efficient Method
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find minimum value to assign all array elements so that array product becomes greater
- Queries to replace every array element by its XOR with a given value with updates
- Replace every array element by Bitwise Xor of previous and next element

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.