We have discussed space efficient implementation of 2 stacks in a single array. In this post, a general solution for k stacks is discussed. Following is the detailed problem statement.
Create a data structure kStacks that represents k stacks. Implementation of kStacks should use only one array, i.e., k stacks should use the same array for storing elements. Following functions must be supported by kStacks.
push(int x, int sn) –> pushes x to stack number ‘sn’ where sn is from 0 to k-1
pop(int sn) –> pops an element from stack number ‘sn’ where sn is from 0 to k-1
Method 1 (Divide the array in slots of size n/k)
A simple way to implement k stacks is to divide the array in k slots of size n/k each, and fix the slots for different stacks, i.e., use arr to arr[n/k-1] for first stack, and arr[n/k] to arr[2n/k-1] for stack2 where arr is the array to be used to implement two stacks and size of array be n.
The problem with this method is inefficient use of array space. A stack push operation may result in stack overflow even if there is space available in arr. For example, say the k is 2 and array size (n) is 6 and we push 3 elements to first and do not push anything to second second stack. When we push 4th element to first, there will be overflow even if we have space for 3 more elements in array.
Method 2 (A space efficient implementation)
The idea is to use two extra arrays for efficient implementation of k stacks in an array. This may not make much sense for integer stacks, but stack items can be large for example stacks of employees, students, etc where every item is of hundreds of bytes. For such large stacks, the extra space used is comparatively very less as we use two integer arrays as extra space.
Following are the two extra arrays are used:
1) top: This is of size k and stores indexes of top elements in all stacks.
2) next: This is of size n and stores indexes of next item for the items in array arr. Here arr is actual array that stores k stacks.
Together with k stacks, a stack of free slots in arr is also maintained. The top of this stack is stored in a variable ‘free’.
All entries in top are initialized as -1 to indicate that all stacks are empty. All entries next[i] are initialized as i+1 because all slots are free initially and pointing to next slot. Top of free stack, ‘free’ is initialized as 0.
Following is implementation of the above idea.
Popped element from stack 2 is 45 Popped element from stack 1 is 39 Popped element from stack 0 is 7
Time complexities of operations push() and pop() is O(1).
The best part of above implementation is, if there is a slot available in stack, then an item can be pushed in any of the stacks, i.e., no wastage of space.
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Queue using Stacks
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Implement two stacks in an array
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)
- Implement Stack using Queues
- Find the nearest smaller numbers on left side in an array
- Find maximum of minimum for every window size in a given array
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Implement a stack using single queue
- Find next Smaller of next Greater in an array
- Find maximum sum possible equal sum of three stacks
- Maximum sum of smallest and second smallest in an array
- How to implement stack using priority queue or heap?
- Bubble sort using two Stacks
- Stack Permutations (Check if an array is stack permutation of other)
- Check if an array is stack sortable
- Growable array based stack
- Implement Stack and Queue using Deque
- Sum of maximum elements of all possible sub-arrays of an array
- Delete array elements which are smaller than next or become smaller