Easy O(n) solution using stack just go thorough it happy coding!

interview-questions
Tags: #<Tag:0x00007f24288c4ad0>

#1

stacks;
vectorv;
s.push(-1);
for(int i=0;i<A.size();i++){
if(s.empty()){
v.push_back(-1);
s.push(A[i]);
}
else{
if(s.top()<A[i]){
v.push_back(s.top());
s.push(A[i]);
}
else{
while(s.top()>=A[i]){
s.pop();
}
v.push_back(s.top());
s.push(A[i]);
}
}
}
return v;