Topics

Problem
Given an array, print the Next Greater Element (NGE) for every element.
The Next Greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1.

#define all(x) x.begin(), x.end()
#define pb push_back
 
vector<int> nextLargerElement(vector<int> arr) {
  int n = arr.size();
  vector<int> res(n);
  stack<int> st;
 
  int x, i;
  for (i = n - 1; i >= 0; i--) {
    x = arr[i];
    
    while (!st.empty()) {
      if (st.top() > x)
        break;
      st.pop();
    }
 
    if (st.empty()) {
      res[i] = -1;
    } else {
      res[i] = st.top();
    }
    
    st.push(x);
  }
  return res;
}
 

T.C: Each element is pushed and popped from the stack atmost once.
S.C: