Get min at pop | Practice | GeeksforGeeks

Now, we'll try to solve a famous stack problem. 
You are given an array A of size N. You need to first push the elements of the array into a stack and then print minimum in the stack at each pop.
Example 1:
Input:
N = 5
A = {1 2 3 4 5}
Output: 
1 1 1 1 1
Explanation: 
After pushing elements to the stack, 
the stack will be "top -> 5, 4, 3, 2, 1". 
Now, start popping elements from the stack
popping 5: min in the stack is 1.popped 5
popping 4: min in the stack is 1. popped 4
popping 3: min in the stack is 1. popped 3
popping 2: min in the stack is 1. popped 2
popping 1: min in the stack is 1. popped 1

Example 2:
Input: 
N = 7
A = {1 6 43 1 2 0 5}
Output: 
0 0 1 1 1 1 1
Explanation: 
After pushing the elements to the stack, 
the stack will be 5->0->2->1->43->6->1. 
Now, poping the elements from the stack:
popping 5: min in the stack is 0. popped 5
popping 0: min in the stack is 0. popped 0
popping 2: min in the stack is 1. popped 2
popping 1: min in the stack is 1. popped 1
popping 43: min in the stack is 1. 
            popped 43
popping 6: min in the stack is 1. popped 6
popping 1: min in the stack is 1. popped 1.


 
Your Task:
Since this is a function problem, you don't need to take any input. Just complete the provided functions _push() and _getMinAtPop(). The _push() function takes an array as parameter, you need to push all elements of this array onto a stack and return the stack. The _getMinAtPop() accepts a stack as a parameter which is returned by _push() function and prints minimum in the stack at each pop separated by spaces.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 <= Ai <= 107
0
kirtikumaricdac5 days ago
//User function Template for Java

class GetMin
{
   //Function to push all the elements into the stack.
 public  static PriorityQueue<Integer> min=new PriorityQueue<Integer>();
   public static Stack<Integer> _push(int arr[],int n)
   {
       // your code here
       Stack<Integer> stack=new Stack<>();
       for(int i=0;i<arr.length;i++)
       {
           
           min.add(arr[i]);
           stack.push(arr[i]);
       }
       return stack;
       
   }
   
   //Function to print minimum value in stack each time while popping.
   static void _getMinAtPop(Stack<Integer>s)
   {
       // your code here
       while(!s.isEmpty())
       {
           // System.out.println(s.peek());
           
          if(min.peek()==s.peek())
          {
              System.out.print(min.peek()+" ");
              min.poll();
          }
          else
          {
              System.out.print(min.peek()+" ");
              min.remove(s.peek());
          }
          s.pop();
       }
       min.clear();
   }
}
0
avfriendscc1 week ago
//best solution c++, TC= O(N), AS= O(N)
stack<int> nearestMin;
stack<int> _push(int arr[],int n){   stack<int> s;   s.push(arr[0]);   nearestMin.push(arr[0]);      for(int i=1; i<n; i++){              if(arr[i]<= nearestMin.top()){           nearestMin.push(arr[i]);       }                  s.push(arr[i]);   }      return s;}
//Function to print minimum value in stack each time while popping.void _getMinAtPop(stack<int>s){   while(s.empty() != true){              cout<<nearestMin.top()<<" ";              if(s.top()== nearestMin.top())           nearestMin.pop();       s.pop();   }}
0
pooja13082 weeks ago
C++ Solution Using Vectors
 
stack<int> _push(int arr[],int n)
{
stack<int> s;
for(int i=0;i<n;i++)
{
    s.push(arr[i]);
}
return s;
}

void _getMinAtPop(stack<int>s)
{
int n=s.size();
 vector<int> v;
   for(int i=0;i<n;i++){
       v.push_back(s.top());
       s.pop();
   
   }

  for(int i=0;i<n;i++)
   {
       cout<<*min_element(v.begin(),v.end())<<" ";
       v.erase(v.begin());
   }
}
 
0
jraveen19971 month ago
TOTAL TIME TAKEN:0.0
#Function to push all the elements into the stack.
def _push(a,n):
    # code here
    stack = []
    while len(a)!=0:
        stack.append(a.pop())
        
    return stack
#Function to print minimum value in stack each time while popping.    
def _getMinAtPop(stack):
    
    # code here
    while len(stack)!=0:
        print(min(stack),end=" ")
        stack.pop(0)
    
+2
lakshta191 month ago
C++ sol
stack<int> _push(int arr[],int n)
{  stack<int>s;
   for(int i=0;i<n;i++){
       s.push(arr[i]);
   }
   return s;
}
int getMin(stack<int>s){
    int ans=INT_MAX;
    while(!s.empty()){
        ans=min(ans,s.top());
            s.pop();
        }
   return ans; 
}
void _getMinAtPop(stack<int>s)
{
    int x;
    while(!s.empty()){
        x=getMin(s);
        s.pop();
        cout<<x<<" ";
    }  
}
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.3/1.4 SEC)
class GetMin{   //Function to push all the elements into the stack.   public static Stack<Integer> _push(int arr[],int n)   {       Stack<Integer> stack=new Stack<Integer>();       for(int i=0;i<n;i++)       {           stack.push(arr[i]);       }       return stack;   }      //Function to print minimum value in stack each time while popping.   static void _getMinAtPop(Stack<Integer>s)   {           while(!s.isEmpty())       {           int min=Integer.MAX_VALUE;           for(int x : s)           {               if(x<min)               {                   min=x;               }           }         s.pop();        System.out.print(min+" ");       }       }}
0
gfgselfpaceddsa1 month ago
//directly calculating min stack 
public static Stack<Integer> _push(int arr[],int n)   {       // your code here       Stack<Integer> min1 = new Stack<>();       int min=Integer.MAX_VALUE;       for(int i=0;i<n;i++)       {           min=Math.min(min,arr[i]);           min1.push(min);                 }       return min1;   }      //Function to print minimum value in stack each time while popping.   static void _getMinAtPop(Stack<Integer>s)   {       // your code here       while(!s.isEmpty())       {           System.out.print(s.pop()+" ");       }          }
+1
lokeshdohare1231 month ago
JAVA Code:
 
public static Stack<Integer> _push(int arr[],int n)   {       Stack<Integer> stack = new Stack<>();       for(int i=0;i<n;i++)           stack.push(arr[i]);       return stack;   }      //Function to print minimum value in stack each time while popping.   static void _getMinAtPop(Stack<Integer>s)   {       ArrayList<Integer> arr = new ArrayList<Integer>();       while(s.isEmpty()==false)       {           arr.add(s.pop());       }       while(arr.size()>0)       {           System.out.print(Collections.min(arr)+" ");           arr.remove(0);       }   }
 
0
chandannayak42071 month ago
Java Solution
class GetMin{   static Stack<Integer> mst = new Stack<>();   public static Stack<Integer> _push(int arr[],int n)   {       Stack<Integer> st = new Stack<>();       mst.push(arr[0]);       for(int i = 0 ; i < n ; i++){           st.push(arr[i]);                      if(mst.peek() >= arr[i]){               mst.push(arr[i]);           }       }              return st;   }      static void _getMinAtPop(Stack<Integer>s)   {              while(s.size() != 0 ){                      int n = s.pop();           System.out.print( mst.peek() + " ");                      if(mst.peek() == n)           mst.pop();                  }   }}
+1
abhijeet194032 months ago
Put all the lements in a list
Put the last element of the list in a min stack
Traverse the list fron n-2 element and keep pushing the element in the min stack depending upon if it less or greater than current minimum
 
***********Code Alert***********
***********Code Alert***********
***********Code Alert***********
***********Code Alert***********
 
//Function to push all the elements into the stack.stack<int> _push(int arr[],int n){  // your code here  stack<int> s;  for(int i=0;i<n;i++)  s.push(arr[i]);    return s;}
//Function to print minimum value in stack each time while popping.void _getMinAtPop(stack<int>s){   // store the elements in a vector      vector<int> vec;   while(s.empty()==false)   {       vec.push_back(s.top());       s.pop();   }   // make a new stack and push the last element of the vector   stack<int> min;   min.push(vec.back());   // keep pushing the elements in the min stack    for(int i=vec.size()-2;i>=0;i--)   {       if(vec[i]<min.top())       min.push(vec[i]);       else       min.push(min.top());   }   // print the values in the stack   while(min.empty()==false)   {       cout << min.top()<<" ";       min.pop();   }} 
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.