Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Function to find the next greater element for each element of the array.
- //Function to find the next greater element for each element of the array.
- vector<long long> nextLargerElement(vector<long long> arr, int n){
- // Your code here
- vector<long long> res;
- stack<long long> st;
- int i;
- //st.push(arr[0]);
- for(i=n-1; i>=0 ; i--)
- {
- while( st.empty()==false && st.top()<= arr[i])
- st.pop();
- if( !st.empty() && st.top() > arr[i]){
- res.push_back(st.top());
- }
- else
- res.push_back(-1);
- st.push(arr[i]);
- }
- reverse(res.begin(), res.end());
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement