Check if all array elements are present in a given stack or not | C ++ program of the above approach ; Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkArrInStack ( stack < int > s , int arr [ ] , int n ) { map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; while ( ! s . empty ( ) ) { int poppedEle = s . top ( ) ; s . pop ( ) ; if ( freq [ poppedEle ] ) freq [ poppedEle ] -= 1 ; } if ( freq . size ( ) == 0 ) return 0 ; return 1 ; } 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 STRNEWLINE " ; else cout << " NO STRNEWLINE " ; }