# Closest greater or same value on left side for every element in array

Given an array of integers, find the closest (not considering distance, but value) greater or same value on left of every element. If an element has no greater or same value on left side, print -1.

**Examples:**

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

Output : -1, 10, -1, 10, -1, 20

First element has nothing on left side, so answer for first is -1.

Second element 5 has 10 on left, so answer is 10.

Third element 11 has nothing greater or same, so answer is -1.

Fourth element 6 has 10 as value wise closes, so answer is 10

Similarly we get values for fifth and sixth elements.Input : arr[] = {10, 5, 11, 10, 20, 12}

Output : -1, 10, -1, 10, -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 toward left of it and find the closest (value wise) greater element. Time complexity of this solution is O(n*n)

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.

We use lower_bound() in C++ to find closest greater element. This function works in Log n time for a set.

## C++

`// C++ implementation of efficient algorithm to find ` `// greater or same element on left side ` `#include <iostream> ` `#include <set> ` `using` `namespace` `std; ` ` ` `// Prints greater elements on left side of every element ` `void` `printPrevGreater(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `set<` `int` `> s; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// First search in set ` ` ` `auto` `it = s.lower_bound(arr[i]); ` ` ` `if` `(it == s.end()) ` `// If no greater found ` ` ` `cout << ` `"-1"` `<< ` `" "` `; ` ` ` `else` ` ` `cout << *it << ` `" "` `; ` ` ` ` ` `// Then insert ` ` ` `s.insert(arr[i]); ` ` ` `} ` `} ` ` ` `/* Driver program to test insertion sort */` `int` `main() ` `{ ` ` ` `int` `arr[] = {10, 5, 11, 10, 20, 12}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `printPrevGreater(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of efficient algorithm

# to find greater or same element on left side

# Prints greater elements

# on left side of every element

def printPrevGreater(arr, n):

s = set()

for i in range(0, n):

# First search in set

it = [x for x in s if x >= arr[i]]

if len(it) == 0: # If no greater found

print(“-1″, end = ” “)

else:

print(min(it), end = ” “)

# Then insert

s.add(arr[i])

# Driver Code

if __name__ == “__main__”:

arr = [10, 5, 11, 10, 20, 12]

n = len(arr)

printPrevGreater(arr, n)

# This code is contributed by Rituraj Jain

**Output:**

-1 10 -1 10 -1 20

Time Complexity : O(n Log n)

## Recommended Posts:

- Find closest greater value for every element in array
- Closest greater element for every array element from another array
- Replace every element with the smallest element on its left side
- Replace every element with the greatest element on its left side
- Smallest Greater Element on Right Side
- Find the nearest smaller numbers on left side in an array
- Ceiling in right side for every element in an array
- Find closest value for every element in array
- Find closest smaller value for every element in array
- Find an element in array such that sum of left array is equal to sum of right array
- Elements greater than the previous and next element in an Array
- First strictly greater element in a sorted array in Java
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
- Rearrange an array such that every odd indexed element is greater than it previous

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.