# Ceiling of every element in same array

Given an array of integers, find the closest smaller or same element for every element. If all elements are greater for an element, then print -1

Examples:

Input : arr[] = {10, 5, 11, 10, 20, 12}
Output : 10 10 12 10 -1 20
Note that there are multiple occurrences of 10, so ceiling of 10 is 10 itself.

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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)

An better solution is to sort the array and create a sorted copy, then do binary search for floor. We traverse the array, for every element we search for the first greater element. In C++ upper_bound() serves this purpose.

Below is the implementation of above approach.

## C++

 `// C++ implementation of efficient algorithm to find ` `// floor of every element ` `#include ` `using` `namespace` `std; ` ` `  `// Prints greater elements on left side of every element ` `void` `printPrevGreater(``int` `arr[], ``int` `n) ` `{ ` `    ``if` `(n == 1) { ` `        ``cout << ``"-1"``; ` `        ``return``; ` `    ``} ` ` `  `    ``// Create a sorted copy of arr[] ` `    ``vector<``int``> v(arr, arr + n); ` `    ``sort(v.begin(), v.end()); ` ` `  `    ``// Traverse through arr[] and do binary search for ` `    ``// every element. ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Find the first element that is greater than ` `        ``// the given element ` `        ``auto` `it = upper_bound(v.begin(), v.end(), arr[i]); ` ` `  `        ``// Since arr[i] also exists in array, *(it-1) ` `        ``// will be same as arr[i]. Let us check *(it-2) ` `        ``// is also same as arr[i]. If true, then arr[i] ` `        ``// exists twice in array, so ceiling is same ` `        ``// same as arr[i] ` `        ``if` `((it - 1) != v.begin() && *(it - 2) == arr[i]) { ` ` `  `            ``// If next element is also same, then there ` `            ``// are multiple occurrences, so print it ` `            ``cout << arr[i] << ``" "``; ` `        ``} ` ` `  `        ``else` `if` `(it != v.end()) ` `            ``cout << *it << ``" "``; ` `        ``else` `            ``cout << -1 << ``" "``; ` `    ``} ` `} ` ` `  `/* Driver program to test insertion sort */` `int` `main() ` `{ ` `    ``int` `arr[] = {10, 5, 11, 10, 20, 12}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``printPrevGreater(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of efficient algorithm to find ` `// floor of every element ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Prints greater elements on left side of every element ` `    ``static` `void` `printPrevGreater(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``if` `(n == ``1``)  ` `        ``{ ` `            ``System.out.println(``"-1"``); ` `            ``return``; ` `        ``} ` ` `  `        ``// Create a sorted copy of arr[] ` `        ``int` `v[] = Arrays.copyOf(arr, arr.length); ` `        ``Arrays.sort(v); ` ` `  `        ``// Traverse through arr[] and do binary search for ` `        ``// every element. ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` ` `  `            ``// Find the first element that is greater than ` `            ``// the given element ` `            ``int` `it = Arrays.binarySearch(v,arr[i]); ` `            ``it++; ` ` `  `            ``// Since arr[i] also exists in array, *(it-1) ` `            ``// will be same as arr[i]. Let us check *(it-2) ` `            ``// is also same as arr[i]. If true, then arr[i] ` `            ``// exists twice in array, so ceiling is same ` `            ``// same as arr[i] ` `            ``if` `((it - ``1``) != ``0` `&& v[it - ``2``] == arr[i]) ` `            ``{ ` ` `  `                ``// If next element is also same, then there ` `                ``// are multiple occurrences, so print it ` `                ``System.out.print(arr[i] + ``" "``); ` `            ``} ` `            ``else` `if` `(it != v.length) ` `                ``System.out.print(v[it] + ``" "``); ` `            ``else` `                ``System.out.print(-``1` `+ ``" "``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``10``, ``5``, ``11``, ``10``, ``20``, ``12``}; ` `        ``int` `n = arr.length; ` `        ``printPrevGreater(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// Rajnis09 `

## Python3

 `# Python implementation of efficient algorithm ` `# to find floor of every element ` `import` `bisect ` ` `  `# Prints greater elements on left side of every element ` `def` `printPrevGreater(arr, n): ` ` `  `    ``if` `n ``=``=` `1``: ` `        ``print``(``"-1"``) ` `        ``return` `     `  `    ``# Create a sorted copy of arr[] ` `    ``v ``=` `list``(arr) ` `    ``v.sort() ` ` `  `    ``# Traverse through arr[] and do binary search for ` `    ``# every element ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# Find the location of first element that ` `        ``# is greater than the given element ` `        ``it ``=` `bisect.bisect_right(v, arr[i]) ` ` `  `        ``# Since arr[i] also exists in array, v[it-1] ` `        ``# will be same as arr[i]. Let us check v[it-2] ` `        ``# is also same as arr[i]. If true, then arr[i] ` `        ``# exists twice in array, so ceiling is same ` `        ``# same as arr[i] ` `        ``if` `(it``-``1``) !``=` `0` `and` `v[it``-``2``] ``=``=` `arr[i]: ` ` `  `            ``# If next element is also same, then there ` `            ``# are multiple occurrences, so print it ` `            ``print``(arr[i], end``=``" "``) ` `         `  `        ``elif` `it <``=` `n``-``1``: ` `            ``print``(v[it], end``=``" "``) ` `         `  `        ``else``: ` `            ``print``(``-``1``, end``=``" "``) ` ` `  ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[``10``, ``5``, ``11``, ``10``, ``20``, ``12``] ` `    ``n ``=` `len``(arr) ` `    ``printPrevGreater(arr, n) ` ` `  `# This code is contributed by  ` `# sanjeev2552 `

## C#

 `// C# implementation of efficient algorithm  ` `// to find floor of every element ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `    ``// Prints greater elements on left side ` `    ``// of every element ` `    ``static` `void` `printPrevGreater(``int` `[]arr, ``int` `n)  ` `    ``{ ` `        ``if` `(n == 1)  ` `        ``{ ` `            ``Console.Write(``"-1"``); ` `            ``return``; ` `        ``} ` ` `  `        ``// Create a sorted copy of arr[] ` `        ``int` `[]v = ``new` `int``[arr.GetLength(0)]; ` `        ``Array.Copy(arr, v, arr.GetLength(0)); ` `        ``Array.Sort(v); ` ` `  `        ``// Traverse through arr[] and  ` `        ``// do binary search for every element. ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` ` `  `            ``// Find the first element that is ` `            ``// greater than the given element ` `            ``int` `it = Array.BinarySearch(v, arr[i]); ` `            ``it++; ` ` `  `            ``// Since arr[i] also exists in array, *(it-1) ` `            ``// will be same as arr[i]. Let us check *(it-2) ` `            ``// is also same as arr[i]. If true, then arr[i] ` `            ``// exists twice in array, so ceiling is same ` `            ``// same as arr[i] ` `            ``if` `((it - 1) != 0 && v[it - 2] == arr[i]) ` `            ``{ ` ` `  `                ``// If next element is also same, then there ` `                ``// are multiple occurrences, so print it ` `                ``Console.Write(arr[i] + ``" "``); ` `            ``} ` `            ``else` `if` `(it != v.Length) ` `                ``Console.Write(v[it] + ``" "``); ` `            ``else` `                ``Console.Write(-1 + ``" "``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `[]arr = {10, 5, 11, 10, 20, 12}; ` `        ``int` `n = arr.Length; ` `        ``printPrevGreater(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```10 10 12 10 -1 20
```

Time Complexity : O(n Log n)
Auxiliary Space : O(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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.