We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Visualize a stack and follow the steps-
Push(2) Top element=2, max=2
Push(1) Top element=1, max=2
Pop() Element=1 is popped, top=2, max=2
print() Prints 2
Above solution has smartly implemented same the logic. It overwrites any smaller value with the max value and then pushes it on top of stack.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
Visualize a stack and follow the steps- Push(2) Top element=2, max=2 Push(1) Top element=1, max=2 Pop() Element=1 is popped, top=2, max=2 print() Prints 2
Above solution has smartly implemented same the logic. It overwrites any smaller value with the max value and then pushes it on top of stack.