Ans: 20 bytes
Applying efficient algorithm,
Main_Stack |
Max_Stack |
5 |
|
8 |
8 |
6 |
|
7 |
7 |
6 |
6 |
5 |
5 |
Here, when 7 is pushed, new entry is created in MAX_STACK because it is the new MAX.
Next element to be pushed is 6. Applying efficient algorithm there is no need to push it in MAX_STACK.
Similarly, when 8 is pushed, new entry is created in MAX_STACK of new MAX.
Now, coming to POP peration, 8,6,7 are popped from MAIN_STACK and along with it 8 and 7 are also popped from MAX_STACK.
At the end, 5 is pushed on MAIN_STACK and no need to insert it on MAX_STACK.
So, final table is -
MAIN_STACK |
MAX_STACK |
5 |
|
6 |
6 |
5 |
5 |
So, total number of elements = 5
Total size = 5*4 bytes = 20 Bytes
PS : Above concept is discussed in Data Structures and Algorithms - Narasimha Karumanchi