Given a stack of integers S and an array of integers arr[], the task is to check if all the array elements are present in the stack or not.
Examples:
Input: S = {10, 20, 30, 40, 50}, arr[] = {20, 30}
Output: Yes
Explanation:
Elements 20 and 30 are present in the stack.Input: S = {50, 60}, arr[] = {60, 50}
Output: Yes
Explanation:
Elements 50 and 60 are present in the stack.
Approach: The idea is to maintain the frequency of array elements in a Hashmap. Now, while the stack is not empty, keep popping the elements out from the stack and reduce the frequency of elements from the Hashmap. Finally, when the stack is empty, check that the frequency of every element in the hash-map is zero or not. If found to be true, print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
// C++ program of the above approach #include<bits/stdc++.h> using namespace std; // Function to check if all array // elements is present in the stack bool checkArrInStack(stack<int>s, int arr[], int n) { map<int, int>freq; // Store the frequency // of array elements for(int i = 0; i < n; i++) freq[arr[i]]++; // Loop while the elements in the // stack is not empty while (!s.empty()) { int poppedEle = s.top(); s.pop(); // Condition to check if the // element is present in the stack if (freq[poppedEle]) freq[poppedEle] -= 1; } if (freq.size() == 0) return 0; return 1; } // Driver code int main() { stack<int>s; s.push(10); s.push(20); s.push(30); s.push(40); s.push(50); int arr[] = {20, 30}; int n = sizeof arr / sizeof arr[0]; if (checkArrInStack(s, arr, n)) cout << "YES\n"; else cout << "NO\n"; } // This code is contributed by Stream_Cipher |
Python3
# Python program of # the above approach # Function to check if all array # elements is present in the stack def checkArrInStack(s, arr): freq = {} # Store the frequency # of array elements for ele in arr: freq[ele] = freq.get(ele, 0) + 1 # Loop while the elements in the # stack is not empty while s: poppedEle = s.pop() # Condition to check if the # element is present in the stack if poppedEle in freq: freq[poppedEle] -= 1 if not freq[poppedEle]: del freq[poppedEle] if not freq: return True return False # Driver Code if __name__ == "__main__": s = [10, 20, 30, 40, 50] arr = [20, 30] if checkArrInStack(s, arr): print("YES") else: print("NO") |
YES
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Elements to be added so that all elements of a range are present in array
- Stack Permutations (Check if an array is stack permutation of other)
- Find elements which are present in first array and not in second
- Count elements present in first array but not in second
- Check if the given push and pop sequences of Stack is valid or not
- Queries to check whether a given digit is present in the given Range
- k-th missing element in increasing sequence which is not present in a given sequence
- Smallest element greater than X not present in the array
- Check if Array elements can be maximized upto M by adding all elements from another array
- First string from the given array whose reverse is also present in the same array
- Check if all elements of the array are palindrome or not
- Check if all array elements are pairwise co-prime or not
- Check if moves in a stack or queue are possible or not
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : Stream_Cipher

