Check if all array elements are present in a given stack or not
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++
#include<bits/stdc++.h>
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\n" ;
else
cout << "NO\n" ;
}
|
Java
import java.util.*;
class GFG{
static boolean checkArrInStack(Stack<Integer>s,
int arr[], int n)
{
HashMap<Integer,
Integer>freq = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++)
if (freq.containsKey(arr[i]))
freq.put(arr[i], freq.get(arr[i]) + 1 );
else
freq.put(arr[i], 1 );
while (!s.isEmpty())
{
int poppedEle = s.peek();
s.pop();
if (freq.containsKey(poppedEle))
freq.put(poppedEle, freq.get(poppedEle) - 1 );
}
if (freq.size() == 0 )
return false ;
return true ;
}
public static void main(String[] args)
{
Stack<Integer> s = new Stack<Integer>();
s.add( 10 );
s.add( 20 );
s.add( 30 );
s.add( 40 );
s.add( 50 );
int arr[] = { 20 , 30 };
int n = arr.length;
if (checkArrInStack(s, arr, n))
System.out.print( "YES\n" );
else
System.out.print( "NO\n" );
}
}
|
Python3
def checkArrInStack(s, arr):
freq = {}
for ele in arr:
freq[ele] = freq.get(ele, 0 ) + 1
while s:
poppedEle = s.pop()
if poppedEle in freq:
freq[poppedEle] - = 1
if not freq[poppedEle]:
del freq[poppedEle]
if not freq:
return True
return False
if __name__ = = "__main__" :
s = [ 10 , 20 , 30 , 40 , 50 ]
arr = [ 20 , 30 ]
if checkArrInStack(s, arr):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool checkArrInStack(Stack< int >s,
int []arr, int n)
{
Dictionary< int ,
int >freq = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
if (freq.ContainsKey(arr[i]))
freq[arr[i]] = freq[arr[i]] + 1;
else
freq.Add(arr[i], 1);
while (s.Count != 0)
{
int poppedEle = s.Peek();
s.Pop();
if (freq.ContainsKey(poppedEle))
freq[poppedEle] = freq[poppedEle] - 1;
}
if (freq.Count == 0)
return false ;
return true ;
}
public static void Main(String[] args)
{
Stack< int > s = new Stack< int >();
s.Push(10);
s.Push(20);
s.Push(30);
s.Push(40);
s.Push(50);
int []arr = {20, 30};
int n = arr.Length;
if (checkArrInStack(s, arr, n))
Console.Write( "YES\n" );
else
Console.Write( "NO\n" );
}
}
|
Javascript
<script>
function checkArrInStack(s, arr, n)
{
var freq = new Map();
for ( var i = 0; i < n; i++)
{
if (freq.has(arr[i]))
freq.set(arr[i], freq.get(arr[i])+1)
else
freq.set(arr[i], 1)
}
while (s.length!=0)
{
var poppedEle = s[s.length-1];
s.pop();
if (freq.has(poppedEle))
freq.set(poppedEle, freq.get(poppedEle)-1);
}
if (freq.size == 0)
return 0;
return 1;
}
var s = [];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
var arr = [20, 30];
var n = arr.length;
if (checkArrInStack(s, arr, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
10 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...