
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace Every Element with the Least Greater Element on its Right using C++
To find the least greater element on the right of an array of integers, we need to consider an array of integers. For each element, we need to find an element that is greater than the current element and is the least from all the candidate elements we have. Suppose we have elements such as [5,23,65,31,76,32,87,23,76,32,88].
vector<int> arr = {5,23,65,31,76,32,87,23,76,32,88}; solve(arr);
We can list our requirements and then think over the requirements to find a solution.
We need a data structure(ds) to ?
Adding an element to our ds in a sorted order (every time we add something to our ds, there will be a sorted order)
We need to do an upper_bound in our sorted ds to find the element which is just greater than our current element, which would be the answer, or it will not be defined if no such element exists.
We will traverse the elements from the right.
From the requirements, we have set the best data structure to use.
Why not a multiset? We don't have any requirement to store an element more than once, so we don't mind using a multiset.
Note ? You may like to use 2 for loops or BST, but a set fulfills our needs nicely, so we'll do it with sets in this article.
Example
Following is the C++ implementation to replace every element in an array with the least greater element on its right ?
#include <iostream> #include <set> #include <vector> using namespace std; void solve (vector < int >&arr) { set < int >s; vector < int >ans (arr.size ()); for (int i = arr.size () - 1; i >= 0; i--) { s.insert (arr[i]); auto it = s.upper_bound (arr[i]); if (it == s.end ()) arr[i] = -1; else arr[i] = *it; } } void printArray (vector < int >&arr) { for (int i:arr) cout << i << " "; cout << "\n"; } int main () { vector < int >arr = { 5, 23, 65, 31, 76, 32, 87, 23, 76, 32, 88 }; printArray (arr); solve (arr); printArray (arr); return 0; }
Output
5 23 65 31 76 32 87 23 76 32 88 23 31 76 32 87 76 88 32 88 88 -1
2nd element(23) has some candidate elements = [65,31,76,32,87,76,32,88], which are right of it and greater than 23. Now we need to pick the minimum from all these elements, which is 31, so the answer for the current element becomes 31. So for all other elements. So the elements become
[23 31 76 32 87 76 88 32 88 88 -1]
Conclusion
As we store each element of our array into our set, the space complexity is O(n). The time complexity of our approach is O(n*log(n)) as the upper_bound and insert operations cost log(n), and we are doing it for every element. Overall this problem is purely data structured based. Considering our requirement, choosing the best data structure is all we need to do.