VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implement-two-stacks-in-an-array/

⇱ Implement two Stacks in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement two Stacks in an Array

Last Updated : 20 Apr, 2026

Create a data structure twoStacks that represent two stacks. Implementation of twoStacks should use only one array, i.e., both stacks should use the same array for storing elements. 

Following functions must be supported by twoStacks.

  • push1(int x) --> pushes x to first stack 
  • push2(int x) --> pushes x to second stack
  • pop1() --> pops an element from first stack and return the popped element 
  • pop2() --> pops an element from second stack and return the popped element

Examples:

Input: push1(2), push1(3), push2(4), pop1(), pop2(), pop2()
Output: [3, 4, -1]
Explanation: push1(2) the stack1 will be [2]
push1(3) the stack1 will be [2,3]
push2(4) the stack2 will be [4]
pop1() the popped element will be 3 from stack1 and stack1 will be [2]
pop2() the popped element will be 4 from stack2 and now stack2 is empty
pop2() the stack2 is now empty hence returned -1

Input: push1(1), push2(2), pop1(), push1(3), pop1(), pop1()
Output: [1, 3, -1]
Explanation: push1(1) the stack1 will be [1]
push2(2) the stack2 will be [2]
pop1() the popped element will be 1
push1(3) the stack1 will be [3]
pop1() the popped element will be 3
pop1() the stack1 is now empty hence returned -1

[Naive Approach] Dividing the space into two halves

The idea to implement two stacks is to divide the array into two halves and assign two halves to two stacks, i.e., use arr[0] to arr[n/2] for stack1, and arr[(n/2) + 1] to arr[n-1] for stack2 where arr[] is the array to be used to implement two stacks and size of array be n. 

Follow the steps below to solve the problem:

To implement push1(x):

  • Check whether top1 has reached mid - 1
  • If yes → Stack1 is full (Overflow)
  • Else → increment top1 and insert the element at arr[top1]

To implement push2(x):

  • Check whether top2 has reached size - 1
  • If yes → Stack2 is full (Overflow)
  • Else → increment top2 and insert the element at arr[top2]

To implement pop1():

  • Check whether top1 is equal to -1
  • If yes → Stack1 is empty (Underflow)
  • Else → return arr[top1] and decrement top1

To implement pop2():

  • Check whether top2 is equal to mid - 1
  • If yes → Stack2 is empty (Underflow)
  • Else → return arr[top2] and decrement top2

Output
3 4 -1 

Time Complexity:

  • Both Push operation: O(1)
  • Both Pop operation: O(1)

Auxiliary Space: O(n), Use of array to implement stack.

Problem in the above implementation

The problem with the approach is that we divide the array into two fixed halves, with one half reserved for stack1 and the other half for stack2. This can lead to inefficient space usage because if stack1 fills up, it cannot use the space available in the second half of the array for stack2, even if that space is not fully utilized.

To fix this, we should allow both stacks to grow dynamically towards each other. Instead of reserving a fixed half for each stack, stack1 will start from the left side of the array, and stack2 will start from the right side. They will grow towards each other. This way, if one stack fills up, the other stack can still use the remaining space. Overflow will only occur when both stacks meet in the middle.

[Expected Approach] Starting from endpoints

The idea is to start two stacks from two extreme corners of arr[]. 

Follow the steps below to solve the problem:

  • Stack1 starts from the leftmost corner of the array, the first element in stack1 is pushed at index 0 of the array. 
  • Stack2 starts from the rightmost corner of the array, the first element in stack2 is pushed at index (n-1) of the array. 
  • Both stacks grow (or shrink) in opposite directions. 
  • To check for overflow, we ensure that there is at least one free space between the two stacks, i.e., top1 < top2 - 1. If this condition fails, the stacks have collided and overflow occurs.
  • To check for underflow:
    Stack1 is empty when top1 == -1
    Stack2 is empty when top2 == size

Output
3 4 -1 

Time Complexity:

  • Both Push operation: O(1)
  • Both Pop operation: O(1)

Auxiliary Space: O(n), Use of the array to implement stack.

Comment