Untitled
unknown
plain_text
2 years ago
388 B
8
Indexable
#include<stack>
vector<int> nextSmallerElement(vector<int> &arr, int n)
{
stack<int> s;
s.push(-1);
vector<int> ans(n);
for(int i=n-1; i>=0 ; i--) {
int curr = arr[i];
while(s.top() >= curr)
{
s.pop();
}
//ans is stack ka top
ans[i] = s.top();
s.push(curr);
}
return ans;
}Editor is loading...