how the stack has stored the index of the values?
CPP - Stock Span Problem
@17manishms
The stack is made of integers so you can insert any int data into it. Here for this particular problem , we find that it is better to store the indices of the elements in the stack rather than the element itself. Since if we have the index , we can directly access the element through the array but cannot do vice versa without performing a search everytime. We simply push the indices of the elements into the stack as
s.push(day) ;
where day is the index of the current element.
To access the element , we can access it as
int index = s.top();
cout << arr [ index ] ;
The stack holds indices so we take the top index and access the element corresponding to it in the array. You can also perform the above operation in a single statement like
cout << arr [ s.top() ] ;
This is how its done in the tutorial video.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.