[Solved] Minimum At Pop Contest Problem

Now, we’ll try to solve a famous stack problem.

You are given an array A of size N. You need to push the elements of the array into a stack and then print minimum in the stack at each pop

Input Format:
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line of input contains size of array N. The second line of array contains the elements of array separated by spaces.

Output Format:
For each testcase, in a new line, print the required output.

Your Task:
Since this is a function problem, you don’t need to take any input. Just complete the provided functions _push() and _getMinAtPop().

Constraints:
1 <= T <= 100
1 <= Ai <= 107

Examples:
Input:

2
5
1 2 3 4 5
7
1 6 43 1 2 0 5
Output:
1 1 1 1 1
0 0 1 1 1 1 1

Solution

class GFG
{
    public static Stack<Integer> _push(int arr[],int n)
    {
            Stack <Integer>s = new Stack<>();
        for(int i=0;i<n;i++)
        {
            s.push(arr[i]);
        }
        return s;
    }
    
    static void _getMinAtPop(Stack<Integer>s)
    {
        ArrayList<Integer>v = new ArrayList<>();
    
    
        while(!s.isEmpty())
        {
            v.add(s.peek());
            s.pop();
        }
        Stack<Integer>minStack = new Stack<>();
        s.push(v.get(v.size()-1));
        minStack.push(v.get(v.size()-1));
        for(int i=v.size()-2;i>=0;i--)
        {
            s.push(v.get(i));
            if(s.peek()<minStack.peek())
            minStack.push(s.peek());
            else
            minStack.push(minStack.peek());
        }
    
        while(!s.isEmpty())
        {
            System.out.print(minStack.peek() + " ");
            s.pop();
            minStack.pop();
        }
    }
}
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)
{
    std::vector<int>v ;
    
    
    while(!s.empty())
    {
      v.push_back(s.top());
        s.pop();
    }
    stack<int>minStack;
    s.push(v.back());
    minStack.push(v.back());
    for(int i=v.size()-2;i>=0;i--)
    {
        s.push(v[i]);
        if(s.top()<minStack.top())
        minStack.push(s.top());
        else
        minStack.push(minStack.top());
    }
    
    while(!s.empty())
    {
        cout<<minStack.top()<<" ";
        s.pop();
        minStack.pop();
    }
}
def _push(lst,n):
	stk = []
	for i in lst:
		stk.append(i)
	return stk

def _getMinAtPop(s,n):
	v = []

	for i in range(n):
		x = s.pop()
		v.append(x)

	minstck = []

	s.append(v[-1])
	minstck.append(v[-1])
	for i in range(n-2,-1,-1):
		s.append(v[i])
		if(s[-1]<minstck[-1]):
			minstck.append(s[-1])
		else:
			minstck.append(minstck[-1])

	for i in range(n):
		print(minstck.pop(),end = " ")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *