Given an array of integers, replace every element with the least greater element on its right side in the array. If there are no greater element on right side, replace it with -1.
Input: [8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28] Output: [18, 63, 80, 25, 32, 43, 80, 93, 80, 25, 93, -1, 28, -1, -1]
A naive method is to run two loops. The outer loop will one by one pick array elements from left to right. The inner loop will find the smallest element greater than the picked element on its right side. Finally the outer loop will replace the picked element with the element found by inner loop. The time complexity of this method will be O(n2).
A tricky solution would be to use Binary Search Trees. We start scanning the array from right to left and insert each element into the BST. For each inserted element, we replace it in the array by its inorder successor in BST. If the element inserted is the maximum so far (i.e. its inorder successor doesn’t exists), we replace it by -1.
Below is C++ implementation of above idea –
18 63 80 25 32 43 80 93 80 25 93 -1 28 -1 -1
Worst case time complexity of above solution is also O(n2) as it uses BST. The worst case will happen when array is sorted in ascending or descending order. The complexity can easily be reduced to O(nlogn) by using balanced trees like red-black trees.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Replace elements with absolute difference of smallest element on left and largest element on right
- Replace each element by the difference of the total size of the array and frequency of that element
- Replace every element with the smallest element on its left side
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element with the greatest element on its left side
- Replace every element with the greatest element on right side
- Replace every element of the array by its previous element
- Maximize the number of indices such that element is greater than element to its left
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Longest Subarray with first element greater than or equal to Last element
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Replace every element of the array by its next element
- Find just strictly greater element from first array for each element in second array
- Replace every element with the smallest of the others
- Replace every element of the array by sum of all other elements
- Replace each element of Array with it's corresponding rank
- Replace every array element by sum of previous and next
- Replace every element of the array with BitWise XOR of all other
- Closest greater element for every array element from another array
- Largest element smaller than current element on left for every element in Array