Count the number of pop operations on stack to get each element of the array
Last Updated :
15 Mar, 2023
Prerequisite: Stack, Hashing
Given a stack of N numbers and an array of numbers. Count the number of pop operations required to get each element of the array. Once an element is popped then it’s not pushed back again. Assume that all the elements from the array are present inside the stack initially.
Examples:
Input: N = 5, Stack: 6 4 3 2 1 , Array: 6 3 4 1 2
Output: 1 2 0 2 0
The 1st element of the stack is the same as the array elements. So to get 6, one pop is required i.e. pop 6 from the stack. To get 3, 2 elements will be popped i.e. 4 and 3. To get 4, 4 was already popped, thus we won’t pop more elements. Similarly, to get 1, we will pop 2 elements and for the 2, no pop count will be added.
Approach: This question can be solved easily by using a stack. We will keep popping the elements till we find the element we are searching for. The only hurdle is how to handle the case when the element is already popped and is not present in the stack. For that, we will maintain a hash map. As we pop an element from the stack we will insert that element in the hash map so that if the element comes later in the array we will first check if it’s present inside the hash maps or in other words has been popped out from the stack previously. Otherwise, we will know it’s present inside the stack and we will start popping the elements till we find the required number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countEle(stack< int >& s, int a[], int N)
{
unordered_map< int , bool > mp;
for ( int i = 0; i < N; ++i) {
int num = a[i];
if (mp.find(num) != mp.end())
cout << "0 " ;
else {
int cnt = 0;
while (s.top() != num) {
mp[s.top()] = true ;
s.pop();
cnt++;
}
s.pop();
cnt++;
cout << cnt << " " ;
}
}
}
int main()
{
int N = 5;
stack< int > s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(6);
int a[] = { 6, 3, 4, 1, 2 };
countEle(s, a, N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Stack;
class GFG
{
public static void countEle(Stack<Integer> s,
int [] a, int N)
{
HashMap<Integer,
Boolean> mp = new HashMap<>();
for ( int i = 0 ; i < N; ++i)
{
int num = a[i];
if (mp.containsKey(num))
System.out.print( "0 " );
else
{
int cnt = 0 ;
while (s.peek() != num)
{
mp.put(s.peek(), true );
s.pop();
cnt++;
}
s.pop();
cnt++;
System.out.print(cnt + " " );
}
}
}
public static void main(String[] args)
{
int N = 5 ;
Stack<Integer> s = new Stack<>();
s.add( 1 );
s.add( 2 );
s.add( 3 );
s.add( 4 );
s.add( 6 );
int [] a = { 6 , 3 , 4 , 1 , 2 };
countEle(s, a, N);
}
}
|
Python3
def countEle(s, a, N):
mp = {}
for i in range ( 0 , N):
num = a[i]
if num in mp:
print ( "0" , end = " " )
else :
cnt = 0
while s[ - 1 ] ! = num:
mp[s.pop()] = True
cnt + = 1
s.pop()
cnt + = 1
print (cnt, end = " " )
if __name__ = = "__main__" :
N = 5
s = []
s.append( 1 )
s.append( 2 )
s.append( 3 )
s.append( 4 )
s.append( 6 )
a = [ 6 , 3 , 4 , 1 , 2 ]
countEle(s, a, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void countEle(Stack< int > s,
int [] a, int N)
{
Dictionary< int ,
bool > mp = new Dictionary< int ,
bool >();
for ( int i = 0; i < N; ++i)
{
int num = a[i];
if (mp.ContainsKey(num))
Console.Write( "0 " );
else
{
int cnt = 0;
while (s.Peek() != num)
{
mp.Add(s.Peek(), true );
s.Pop();
cnt++;
}
s.Pop();
cnt++;
Console.Write(cnt + " " );
}
}
}
public static void Main(String[] args)
{
int N = 5;
Stack< int > s = new Stack< int >();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
s.Push(6);
int [] a = { 6, 3, 4, 1, 2 };
countEle(s, a, N);
}
}
|
Javascript
<script>
function countEle(s, a, N)
{
var mp = new Map();
for ( var i = 0; i < N; ++i) {
var num = a[i];
if (mp.has(num))
document.write( "0 " );
else {
var cnt = 0;
while (s[s.length-1] != num) {
mp.set(s[s.length-1], true );
s.pop();
cnt++;
}
s.pop();
cnt++;
document.write( cnt + " " );
}
}
}
var N = 5;
var s = [];
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(6);
var a = [6, 3, 4, 1, 2 ];
countEle(s, a, N);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...