14. Implement two stacks in an array
Last updated
Last updated
The problem can be found at the following link: Question Link
The question is straightforward and involves implementing two stacks using the starting
and ending
of the array arr
. The main operations required are push and pop for these two stacks. Therefore, you only need to complete these functions accordingly. Additionally, the question suggests the need to handle the underflow condition of the stack.
For push1
, we increment top1
and store the element at the updated top1
index of the array.
For push2
, we decrement top2
and store the element at the updated top2
index of the array.
For pop1
, we check if top1
is -1 (indicating an empty stack). If it is, we return -1. Otherwise, we return the element at the top1
index and decrement top1
.
For pop2
, we check if top2
is equal to size
(indicating an empty stack). If it is, we return -1. Otherwise, we return the element at the top2
index and increment top2
.
The visual representation illustrates the appearance of our stacks.
Time Complexity: The time complexity for all the operations (push1
, push2
, pop1
, pop2
) is O(1)
because we are directly accessing the top elements and updating the pointers.
Auxiliary Space Complexity: O(1)
, Since we are not utilising any extra space.
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.