Given a stack of N numbers and an array of numbers. Count the numbers of pop operations required to get each element of the array. Once an element is popped then its not pushed back again. Assume that the all the elements from the array present inside the stack initially.
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. The only hurdle is that 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 its 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 above approach:
1 2 0 2 0
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Design a stack with operations on middle element
- Minimum number of given operations required to reduce the array to 0 element
- Count the number of elements which are greater than any of element on right side of an array
- Reduce the array to atmost one element by the given operations
- Find maximum value of the last element after reducing the array with given operations
- Minimum element left from the array after performing given operations
- Minimum pair sum operations to make array each element divisible by 4
- Minimize operations required to make each element of Array equal to it's index value
- Count the number of operations required to reduce the given number
- Maximum count of equal numbers in an array after performing given operations
- Count of suffix increment/decrement operations to construct a given array
- Count of operations required to update the array such that it satisfies the given conditions
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Number of elements from the array which are reachable after performing given operations on D
- Minimum number of operations on an array to make all elements 0
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.