Design a special dynamic Stack using an array that supports all the stack operations such as push(), pop(), peek(), isEmpty(), and getMin() operations in constant Time and Space complexities.
Examples:
Assuming the right to left orientation as the top to bottom orientation and performing the operations:
- Push(10): 10 is added to the top of the stack. Thereafter, the stack modifies to {10}.
- Push(4): 4 is added to the top of the stack. Thereafter, the stack modifies to {10, 4}.
- Push(9): 9 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9}.
- Push(6): 6 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9, 6}.
- Push(5): 5 is added to the top of the stack. Thereafter, the stack modifies to {10, 4, 9, 6, 5}.
- Peek(): Prints the top element of the stack 5.
- getMin(): Prints the minimum element of the stack 4.
- Pop(): Deletes the top most element, 5 from the stack. Thereafter, the stack modifies to {10, 4, 9, 6}.
- Pop(): Deletes the top most element, 6 from the stack. Thereafter, the stack modifies to {10, 4, 9}.
- Pop(): Deletes the top most element, 9 from the stack. Thereafter, the stack modifies to {10, 4}.
- Pop(): Deletes the top most element, 4 from the stack. Thereafter, the stack modifies to {10}.
- Peek(): Prints the top element of the stack 10.
- getMin(): Prints the minimum element of the stack 10.
Approach: To implement a dynamic stack using an array the idea is to double the size of the array every time the array gets full. Follow the steps below to solve the problem:
- Initialize an array, say arr[] with an initial size 5, to implement the stack.
- Also, initialize two variables, say top and minEle to store the index of the top element of the stack and minimum element of the stack.
- Now, perform the following stack operations:
- isEmpty(): Checks if the stack is empty or not.
- Return true if the top is less or equal to 0. Otherwise, return false.
- Push(x): Inserts x at the top of the stack.
- If the stack is empty, insert x into the stack and make minEle equal to x.
- If the stack is not empty, compare x with minEle. Two cases arise:
- If x is greater than or equal to minEle, simply insert x.
- If x is less than minEle, insert (2*x – minEle) into the stack and make minEle equal to x.
- If the array used is full then, double the size of the array and then copy all the elements of the previous array to the new array and then assign the address of the new array to the original array. Thereafter, perform the push operation as discussed above.
- Pop(): Removes an element from the top of the stack.
- Let the removed element be y. Two cases arise
- If y is greater than or equal to minEle, the minimum element in the stack is still minEle.
- If y is less than minEle, the minimum element now becomes (2*minEle – y), so update minEle as minEle = 2*minEle-y.
- getMin(): Finds the minimum value of the stack.
- If the stack is not empty then return the value of minEle. Otherwise, return “-1” and print “Underflow“.
Illustration:
Push(x)

- Number to be Inserted: 3, Stack is empty, so insert 3 into stack and minEle = 3.
- Number to be Inserted: 5, Stack is not empty, 5> minEle, insert 5 into stack and minEle = 3.
- Number to be Inserted: 2, Stack is not empty, 2< minEle, insert (2*2-3 = 1) into stack and minEle = 2.
- Number to be Inserted: 1, Stack is not empty, 1< minEle, insert (2*1-2 = 0) into stack and minEle = 1.
- Number to be Inserted: 1, Stack is not empty, 1 = minEle, insert 1 into stack and minEle = 1.
- Number to be Inserted: -1, Stack is not empty, -1 < minEle, insert (2*-1 – 1 = -3) into stack and minEle = -1.
Pop()

- Initially the minimum element minEle in the stack is -1.
- Number removed: -3, Since -3 is less than the minimum element the original number being removed is minEle which is -1, and the new minEle = 2*-1 – (-3) = 1
- Number removed: 1, 1 == minEle, so number removed is 1 and minEle is still equal to 1.
- Number removed: 0, 0< minEle, original number is minEle which is 1 and new minEle = 2*1 – 0 = 2.
- Number removed: 1, 1< minEle, original number is minEle which is 2 and new minEle = 2*2 – 1 = 3.
- Number removed: 5, 5> minEle, original number is 5 and minEle is still 3
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Stack {
private :
int Max = 5;
int * arr = new int (Max);
int minEle = 0;
int top = 0;
public :
bool empty()
{
if (top <= 0) {
return true ;
}
else {
return false ;
}
}
void push( int x)
{
if (empty()) {
minEle = x;
arr[top] = x;
top++;
}
else if (top == Max) {
Max = 2 * Max;
int * temp = new int (Max);
for ( int i = 0; i < top; i++) {
temp[i] = arr[i];
}
if (x < minEle) {
temp[top] = 2 * x - minEle;
minEle = x;
top++;
}
else {
temp[top] = x;
top++;
}
arr = temp;
}
else {
if (x < minEle) {
arr[top] = 2 * x - minEle;
top++;
minEle = x;
}
else {
arr[top] = x;
top++;
}
}
}
void pop()
{
if (empty()) {
cout << "Underflow" << endl;
return ;
}
int t = arr[top - 1];
if (t < minEle) {
cout << "Popped element : " << minEle << endl;
minEle = 2 * minEle - t;
}
else {
cout << "Popped element : " << t << endl;
}
top--;
return ;
}
int peek()
{
if (empty()) {
cout << "Underflow" << endl;
return -1;
}
int t = arr[top - 1];
if (t < minEle) {
return minEle;
}
else {
return t;
}
}
int getMin()
{
if (empty()) {
cout << "Underflow" << endl;
return -1;
}
else {
return minEle;
}
}
};
int main()
{
Stack S;
S.push(10);
S.push(4);
S.push(9);
S.push(6);
S.push(5);
cout << "Top Element : " << S.peek() << endl;
cout << "Minimum Element : " << S.getMin() << endl;
S.pop();
S.pop();
S.pop();
S.pop();
cout << "Top Element : " << S.peek() << endl;
cout << "Minimum Element : " << S.getMin() << endl;
return 0;
}
|
Java
public class Main
{
static int Max = 5 ;
static int [] arr = new int [Max];
static int minEle = 0 ;
static int Top = 0 ;
static boolean empty()
{
if (Top <= 0 ) {
return true ;
}
else {
return false ;
}
}
static void push( int x)
{
if (empty()) {
minEle = x;
arr[Top] = x;
Top++;
}
else if (Top == Max) {
Max = 2 * Max;
int [] temp = new int [Max];
for ( int i = 0 ; i < Top; i++) {
temp[i] = arr[i];
}
if (x < minEle) {
temp[Top] = 2 * x - minEle;
minEle = x;
Top++;
}
else {
temp[Top] = x;
Top++;
}
arr = temp;
}
else {
if (x < minEle) {
arr[Top] = 2 * x - minEle;
Top++;
minEle = x;
}
else {
arr[Top] = x;
Top++;
}
}
}
static void pop()
{
if (empty()) {
System.out.print( "Underflow" );
return ;
}
int t = arr[Top - 1 ];
if (t < minEle) {
System.out.println( "Popped element : " + minEle);
minEle = 2 * minEle - t;
}
else {
System.out.println( "Popped element : " + t);
}
Top--;
return ;
}
static int peek()
{
if (empty()) {
System.out.println( "Underflow" );
return - 1 ;
}
int t = arr[Top - 1 ];
if (t < minEle) {
return minEle;
}
else {
return t;
}
}
static int getMin()
{
if (empty()) {
System.out.println( "Underflow" );
return - 1 ;
}
else {
return minEle;
}
}
public static void main(String[] args) {
push( 10 );
push( 4 );
push( 9 );
push( 6 );
push( 5 );
System.out.println( "Top Element : " + peek());
System.out.println( "Minimum Element : " + getMin());
pop();
pop();
pop();
pop();
System.out.println( "Top Element : " + peek());
System.out.println( "Minimum Element : " + getMin());
}
}
|
Python3
Max = 5
arr = [ 0 ] * Max
minEle = 0
Top = 0
def empty():
if (Top < = 0 ):
return True
else :
return False
def push(x):
global arr, Top, Max , minEle
if empty():
minEle = x
arr[Top] = x
Top + = 1
elif (Top = = Max ):
Max = 2 * Max
temp = [ 0 ] * Max
for i in range (Top):
temp[i] = arr[i]
if (x < minEle):
temp[Top] = 2 * x - minEle
minEle = x
Top + = 1
else :
temp[Top] = x
Top + = 1
arr = temp
else :
if (x < minEle):
arr[Top] = 2 * x - minEle
Top + = 1
minEle = x
else :
arr[Top] = x
Top + = 1
def pop():
global Top, minEle
if empty():
print ( "Underflow" )
return
t = arr[Top - 1 ]
if (t < minEle) :
print ( "Popped element :" , minEle)
minEle = 2 * minEle - t
else :
print ( "Popped element :" , t)
Top - = 1
return
def peek():
if empty():
print ( "Underflow" )
return - 1
t = arr[Top - 1 ]
if (t < minEle):
return minEle
else :
return t
def getMin():
if empty():
print ( "Underflow" )
return - 1
else :
return minEle
push( 10 )
push( 4 )
push( 9 )
push( 6 )
push( 5 )
print ( "Top Element :" , peek())
print ( "Minimum Element :" , getMin())
pop()
pop()
pop()
pop()
print ( "Top Element :" , peek())
print ( "Minimum Element :" , getMin())
|
C#
using System;
class GFG {
static int Max = 5;
static int [] arr = new int [Max];
static int minEle = 0;
static int Top = 0;
static bool empty()
{
if (Top <= 0) {
return true ;
}
else {
return false ;
}
}
static void push( int x)
{
if (empty()) {
minEle = x;
arr[Top] = x;
Top++;
}
else if (Top == Max) {
Max = 2 * Max;
int [] temp = new int [Max];
for ( int i = 0; i < Top; i++) {
temp[i] = arr[i];
}
if (x < minEle) {
temp[Top] = 2 * x - minEle;
minEle = x;
Top++;
}
else {
temp[Top] = x;
Top++;
}
arr = temp;
}
else {
if (x < minEle) {
arr[Top] = 2 * x - minEle;
Top++;
minEle = x;
}
else {
arr[Top] = x;
Top++;
}
}
}
static void pop()
{
if (empty()) {
Console.WriteLine( "Underflow" );
return ;
}
int t = arr[Top - 1];
if (t < minEle) {
Console.WriteLine( "Popped element : " + minEle);
minEle = 2 * minEle - t;
}
else {
Console.WriteLine( "Popped element : " + t);
}
Top--;
return ;
}
static int peek()
{
if (empty()) {
Console.WriteLine( "Underflow" );
return -1;
}
int t = arr[Top - 1];
if (t < minEle) {
return minEle;
}
else {
return t;
}
}
static int getMin()
{
if (empty()) {
Console.WriteLine( "Underflow" );
return -1;
}
else {
return minEle;
}
}
static void Main() {
push(10);
push(4);
push(9);
push(6);
push(5);
Console.WriteLine( "Top Element : " + peek());
Console.WriteLine( "Minimum Element : " + getMin());
pop();
pop();
pop();
pop();
Console.WriteLine( "Top Element : " + peek());
Console.WriteLine( "Minimum Element : " + getMin());
}
}
|
Javascript
<script>
let Max = 5;
let arr = new Array(Max);
let minEle = 0;
let Top = 0;
function empty()
{
if (Top <= 0) {
return true ;
}
else {
return false ;
}
}
function push(x)
{
if (empty()) {
minEle = x;
arr[Top] = x;
Top++;
}
else if (Top == Max) {
Max = 2 * Max;
let temp = new Array(Max);
for (let i = 0; i < Top; i++) {
temp[i] = arr[i];
}
if (x < minEle) {
temp[Top] = 2 * x - minEle;
minEle = x;
Top++;
}
else {
temp[Top] = x;
Top++;
}
arr = temp;
}
else {
if (x < minEle) {
arr[Top] = 2 * x - minEle;
Top++;
minEle = x;
}
else {
arr[Top] = x;
Top++;
}
}
}
function pop()
{
if (empty()) {
document.write( "Underflow" + "</br>" );
return ;
}
let t = arr[Top - 1];
if (t < minEle) {
document.write( "Popped element : " + minEle + "</br>" );
minEle = 2 * minEle - t;
}
else {
document.write( "Popped element : " + t + "</br>" );
}
Top--;
return ;
}
function peek()
{
if (empty()) {
document.write( "Underflow" + "</br>" );
return -1;
}
let t = arr[Top - 1];
if (t < minEle) {
return minEle;
}
else {
return t;
}
}
function getMin()
{
if (empty()) {
document.write( "Underflow" + "</br>" );
return -1;
}
else {
return minEle;
}
}
push(10);
push(4);
push(9);
push(6);
push(5);
document.write( "Top Element : " + peek() + "</br>" );
document.write( "Minimum Element : " + getMin() + "</br>" );
pop();
pop();
pop();
pop();
document.write( "Top Element : " + peek() + "</br>" );
document.write( "Minimum Element : " + getMin() + "</br>" );
</script>
|
OutputTop Element : 5
Minimum Element : 4
Popped element : 5
Popped element : 6
Popped element : 9
Popped element : 4
Top Element : 10
Minimum Element : 10
Time Complexity: O(1) for each operation
Auxiliary Space: O(1)