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, 10, 20, 12}

Output : 10 10 12 12 -1 -1Input : arr[] = {50, 20, 200, 100, 30}

Output : 100 30 -1 -1 -1

A **simple solution** is to run two nested loops. We pick an outer element one by one. For every picked element, we traverse right side array and find closest greater or equal 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 ceiling operations in O(Log n) time.

## C++

`// C++ program to find ceiling on right side for ` `// every element. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `closestGreater(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `set<` `int` `> s; ` ` ` `vector<` `int` `> ceilings; ` ` ` ` ` `// Find smallest greater or equal element ` ` ` `// for every array element ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` `auto` `greater = s.lower_bound(arr[i]); ` ` ` `if` `(greater == s.end()) ` ` ` `ceilings.push_back(-1); ` ` ` `else` ` ` `ceilings.push_back(*greater); ` ` ` `s.insert(arr[i]); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) ` ` ` `cout << ceilings[i] << ` `" "` `; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 50, 20, 200, 100, 30 }; ` ` ` `closestGreater(arr, 5); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ceiling on right side for ` `// every element. ` `import` `java.util.*; ` ` ` `class` `TreeSetDemo { ` ` ` `public` `static` `void` `closestGreater(` `int` `[] arr) ` ` ` `{ ` ` ` `int` `n = arr.length; ` ` ` `TreeSet<Integer> ts = ` `new` `TreeSet<Integer>(); ` ` ` `ArrayList<Integer> ceilings = ` `new` `ArrayList<Integer>(n); ` ` ` ` ` `// Find smallest greater or equal element ` ` ` `// for every array element ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) { ` ` ` `Integer greater = ts.ceiling(arr[i]); ` ` ` `if` `(greater == ` `null` `) ` ` ` `ceilings.add(-` `1` `); ` ` ` `else` ` ` `ceilings.add(greater); ` ` ` `ts.add(arr[i]); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `System.out.print(ceilings.get(i) + ` `" "` `); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `[] arr = { ` `50` `, ` `20` `, ` `200` `, ` `100` `, ` `30` `}; ` ` ` `closestGreater(arr); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

100 30 -1 -1 -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:

- Closest greater or same value on left side for every element in array
- Replace every element with the least greater element on its right
- Smallest Greater Element on Right Side
- Largest element smaller than current element on left for every element in Array
- TreeSet ceiling() method in Java with Examples
- Find closest smaller value for every element in array
- Find closest greater value for every element in array
- Find closest value for every element in array
- Find just strictly greater element from first array for each element in second array
- Maximum Unique Element in every subarray of size K
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Add all greater values to every node in a given BST
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Remove an Element at specific index from an Array in Java
- Remove all occurrences of an element from Array in Java
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Find k-th smallest element in BST (Order Statistics in BST)
- K'th smallest element in BST using O(1) Extra Space
- K'th Largest Element in BST when modification to BST is not allowed

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.