how to solve this question using stack?
How to solve this question using stack?
@pragyachoudhary1111 hey pragya chaudhary
first take a input array
and a stack< int > s;
push the first index of array into the stack like s.push(a[0]);
apply loop from i=1 to n
now apply a while loop
while(!s.empty()&&s.top()<a[ i ])
{
cout<<a[ i ]<<" “;
now pop the current element from the stack reason for that we want to check for next element
s.pop();
}
push the next element of the array according to loop
s.push( a[ i ] );
while(! s.empty() )
{
cout<<-1<<” ";
s.pop();
}