Special Stack | Practice | GeeksforGeeks

Design a data-structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. Your task is to complete all the functions, using stack data-Structure.

Example 1:
Input:
Stack: 18 19 29 15 16
Output: 15
Explanation:
The minimum element of the stack is 15.

 

Your Task:
Since this is a function problem, you don't need to take inputs. You just have to complete 5 functions, push() which takes the stack and an integer x as input and pushes it into the stack; pop() which takes the stack as input and pops out the topmost element from the stack; isEmpty() which takes the stack as input and returns true/false depending upon whether the stack is empty or not; isFull() which takes the stack and the size of the stack as input and returns true/false depending upon whether the stack is full or not (depending upon the
given size); getMin() which takes the stack as input and returns the minimum element of the stack. 
Note: The output of the code will be the value returned by getMin() function.

Expected Time Complexity: O(N) for getMin, O(1) for remaining all 4 functions.
Expected Auxiliary Space: O(1) for all the 5 functions.

Constraints:
1 ≤ N ≤ 104
0
amonk5 days ago
#include<bits/stdc++.h>
void push(stack<int>& s, int a){
	// Your code goes here
	s.push(a);
}

bool isFull(stack<int>& s,int n){
	// Your code goes here
	return s.size()==n;
}

bool isEmpty(stack<int>& s){
	// Your code goes here
	return s.size()==0;
}

int pop(stack<int>& s){
	// Your code goes here
	int x=s.top();
	s.pop();
	return x;
}

int getMin(stack<int>& s){
	// Your code goes here
	
	int mn=INT_MAX;
	
	while(!s.empty()){
	    mn=min(s.top(),mn);
	    s.pop();
	}
	return mn;
}
0
19059363 weeks ago
void push(stack<int>& s, int a){s.push(a);}
bool isFull(stack<int>& s,int n){if(s.size()==n){    return true;}else{    return false;}}
bool isEmpty(stack<int>& s){if(s.empty()){    return true;}else{    return false;}}
int pop(stack<int>& s){   s.pop();}
int getMin(stack<int>& s){   //INT_MAX VALUE=2,147,483,647 or 1e9   //and for INT_MIN=-2,147,483,648 or -1e9int min=2147483647;if(s.size()==1){    min=s.top();}else{    while(!s.empty()){        if(s.top()<min){            min=s.top();        }       s.pop();    }}return min;}
0
deepcsr43 weeks ago
Java Code
 
class GfG{   public void push(int a,Stack<Integer> s) {       s.push(a);   }
   public int pop(Stack<Integer> s) {       return s.pop();   }
   public int min(Stack<Integer> s) {       int m=pop(s);       while(!isEmpty(s)){           int n=pop(s);           if(n<m)               m=n;       }       return m;   }
   public boolean isFull(Stack<Integer>s, int n)   {       if (s.size() == n) {           return true;       }       return false;   }
   public boolean isEmpty(Stack<Integer> s) {       return s.size() == 0;   }}
+1
amarrajsmart1971 month ago
C++  Easy Solution  time taken =0.0/1.4 sec.void push(stack<int>& s, int a){// Your code goes heres.push(a);}
bool isFull(stack<int>& s,int n){// Your code goes hereif(s.size()==n)return true;return false;}
bool isEmpty(stack<int>& s){// Your code goes hereif(s.empty())return true;return false;}
int pop(stack<int>& s){// Your code goes heres.pop();}
int getMin(stack<int>& s){// Your code goes hereint min_=10000;while(!s.empty()){if(s.top()<min_){    min_=s.top();}s.pop();}return min_;}
 
0
milindprajapatmst191 month ago
Constant time and space solution
Why does this work?
Let us assume _min is the current minimum and x is the element that needs to be inserted into the stack.
Let us say x is the new minimum element (x < _min)
We push 2x - _min into the stack and update _min to x.
Why do we push 2x - _min?
Take this inequality (x < _min)
Rearrange, x - _min < 0
Add x to both sides, 2x - _min < x, where x is the new minimum
As you can see, 2x - _min, the element we will insert, will still be less than the new minimum element, x. So it does not affect the relative order of elements.
Now let's see how this helps us retain the original elements.
Let us assume _min is the current minimum and x is the element at the top.
While we pop the element, we need to check whether the element at the top is the current minimum or not. If so, then we need to update the current minimum as well.
Using inequality (2x - _min < x, where x is the new minimum), we know if the top element is less than the current minimum, then this is the current minimum that is being popped out of the stack. And we need to update the current minimum element.
How can we do it?
Remember, we pushed 2x - _min into the stack, where x is the new minimum, and _min is now the previous minimum.
Rewriting, 2 _min - prev_min
This is what we have on top, so we have its value. And hence prev_min can easily be calculated using, 
2 _min - prev_min = top
prev_min = 2 _min - top

Implementation:

int _min;
void push(stack<int>& s, int a) {
    if (isEmpty(s)) {
        _min = a;
        s.push(a);
    }
    else if (a < _min) {
        s.push(2 * a - _min);
        _min = a;
    }
    else
        s.push(a);
}
bool isFull(stack<int>& s, int n) {
	return s.size() == n;
}
bool isEmpty(stack<int>& s) {
	return s.size() == 0;
}
int pop(stack<int>& s) {
	int top = s.top();
	if (top < _min)
	    _min = 2 * _min - top;
	s.pop();
}
int getMin(stack<int>& s) {
    return _min;
}
0
ravishraj121 month ago
java solution:
class GfG{   Stack<Integer> ss = new Stack<>();public void push(int a,Stack<Integer> s){    //add code here.    s.push(a);    if(ss.size()==0 || a<=ss.peek())        ss.push(a);    return;}public int pop(Stack<Integer> s)       {           //add code here.           if(s.size()==0)               return -1;           int ans = s.peek();               s.pop();           if(ss.peek() == ans)               ss.pop();           return ans;}public int min(Stack<Integer> s)       {          //add code here.          if(ss.size()==0)               return -1;           return ss.peek();}public boolean isFull(Stack<Integer>s, int n)       {          //add code here.          return s.size()==n;}public boolean isEmpty(Stack<Integer>s)       {          //add code here.          return s.size()==0;}}
0
shibombhowmik12091 month ago
//based on constraints we can apply the below algo which //handles positive numbers only.

int min1;

void push(stack<int>& s, int a){
	// Your code goes here
	if(s.empty()){
	    s.push(a);
	    min1=a;
	}
	else if(a<=min1){
	    s.push(a-min1);
	    min1=a;
	}
	else{
	    s.push(a);
	}
}

bool isFull(stack<int>& s,int n){
	// Your code goes here
	return (s.size()==n);
}

bool isEmpty(stack<int>& s){
	// Your code goes here
	return (s.empty());
}

int pop(stack<int>& s){
	// Your code goes here
	int t=s.top();
	s.pop();
	if(t<=0){
	    int res=min1;
	    min1=min1-t;
	    return res;
	}
	
	return t;
}

int getMin(stack<int>& s){
	// Your code goes here
	return min1;
}
0
shubhamnikum21311 month ago
c++ solution
 
void push(stack<int>& s, int a){// Your code goes heres.push(a);}
bool isFull(stack<int>& s,int n){// Your code goes hereif(s.size()==n)    return true;else    return false;}
bool isEmpty(stack<int>& s){// Your code goes hereif(s.empty())    return true;else    return false;}
int pop(stack<int>& s){// Your code goes here   s.pop();}
int getMin(stack<int>& s){// Your code goes hereint ans=10000;if(s.size()==1)    ans=s.top();else{while(!s.empty()){if(s.top()<ans)    ans=s.top();else    s.pop();    }}return ans;
}
-2
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class GfG{
	public void push(int a,Stack<Integer> s){
	    //add code here.
	    s.push(a);
	}
	public int pop(Stack<Integer> s){
        //add code here.
        return s.pop();
	}
	public int min(Stack<Integer> s){
        //add code here.
        int min = Integer.MAX_VALUE;
        Stack<Integer> temp = new Stack<>();
        while(!s.isEmpty()) temp.push(s.pop());
        while(!temp.isEmpty()){
            int curr = temp.pop();
            min = Math.min(min,curr);
            s.push(curr);
        }
        return min;
	}
	public boolean isFull(Stack<Integer>s, int n){
        //add code here.
        return s.size()==n;
	}
	public boolean isEmpty(Stack<Integer>s){
        //add code here.
        return s.size()==0;
	}
}
0
tirtha19025682 months ago
class GfG{
public void push(int a,Stack<Integer> s)
{
    s.push(a);
}
public int pop(Stack<Integer> s)
       {
          return s.pop();
}
public int min(Stack<Integer> s)
       {
          int min = Integer.MAX_VALUE;
          Stack<Integer>stk = new Stack<>();
          while(s.isEmpty()==false){
              if(s.peek() < min){
                  min = s.peek();
              }
              stk.push(s.pop());
          }
          
          while(stk.isEmpty()==false){
              s.push(stk.pop());
          }
          
          return min;
}
public boolean isFull(Stack<Integer>s, int n)
       {
          if(s.size()<n) return false;
          return true;
}
public boolean isEmpty(Stack<Integer>s)
       {
          return s.isEmpty();
}
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.