Count the number of pop operations on stack to get each element of the array in C++

C++Server Side ProgrammingProgramming

Given an array of numbers and a stack. All the elements of the array are present inside the stack The goal is to find the count of pop operations required for getting individual array elements.

The stack is filled in decreasing order, the first element is highest and top element is lowest.

For Example

Input

Stack [ 7,6,2,1 ] array : 2,1,6,7

Output

Count of number of pop operations on stack to get each element of the array
are: 3 1 0 0

Explanation

Traversing array from 0th index, To get 2 we will pop stack three times. So arr[0] is 3. First two pops will give 7 and 6 also. To get 1 we will pop stack once now. So arr[1] is 1. For 6 and 7 as these are already popped, so arr[2]=arr[3]=0.

Input

Stack [ 3,2,1,1 ] array : 1,2,1,3

Output

Count of number of pop operations on stack to get each element of the array
are: 3 0 1 0

Explanation

Traversing array from 0th index, To get 1 we will pop stack three times. So arr[0] is 3. First two pops will give 3 and 2 also.Traversing array from 0th index, To get 1 we will pop stack three times. So arr[0] is 3. First two pops will give 3 and 2 also.

Approach used in the below program is as follows

In this approach we will use an unordered_map<int, bool> um to check if the element is already popped or not. Pop element and add it to um. If it comes again then set popping count as 0. If not then increment count till we get it.

  • Take an integer array arr[].

  • Take a stack<int> stck for storing elements in it.

  • Push elements in it in decreasing order.

  • Function pop_operations(stack<int>& stck, int arr[], int elements) returns count of the number of pop operations on stack to get each element of the array.

  • Take the initial count as 0.

  • Take unordered_map<int, bool> um for storing unique numbers encountered while performing pop operations on stack.

  • Traverse array using for loop.

  • Take temp=arr[i].

  • If temp is not in um. Print 0 pop operations required to get it.

  • Else while temp is not found, perform pop on stack and set each element popped in um as true and increment count.

  • At the end of while, print count.

  • In this way we will print counts of pop operations for each element of the array.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void pop_operations(stack<int>& stck, int arr[], int elements){
   int count = 0;
   unordered_map<int, bool> um;
   cout<<"Count of number of pop operations on stack to get each element of the array are: ";
   for (int i = 0; i < elements; ++i){
      int temp = arr[i];
      if (um.find(temp) != um.end())
      { cout << "0 "; }
      else{
         count = 0;
         while (stck.top() != temp){
            um[stck.top()] = true;
            stck.pop();
            count++;
         }
         stck.pop();
         count++;
         cout<<count<<" ";
      }
   }
}
int main(){
   int elements = 4;
   int arr[] = { 2, 1, 6, 7};
   stack<int> stck;
   stck.push(1);
   stck.push(2);
   stck.push(6);
   stck.push(7);
   pop_operations(stck, arr, elements);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of number of pop operations on stack to get each element of the array are: 3 1 0 0
raja
Updated on 05-Jan-2021 06:57:21

Advertisements