15. Delete middle element of a stack
The problem can be found at the following link: Question Link
My Approach
We can utilize an additional stack to temporarily store the elements while popping until we reach the middle element in the original stack. Here are the steps involved:
Determine the number of elements on top of the middle element by dividing the
sizeOfStack
by 2, and assign the result to the variablecnt
.Iterate
cnt
times and temporarily store the elements in thenewStack
.Pop the middle element from the original stack
s
.Re-push the elements stored in the
newStack
back into the original stacks
.
Time and Auxiliary Space Complexity
Time Complexity:
O(N)
, whereN
is the number of elements in the stack.Auxiliary Space Complexity:
O(N)
, as we are using an auxiliary stack to temporarily store elements.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated