Skip to main content

Array Subset of another array [EASY-Hash-set]

a)Company: Qualcomm, GE

Question:

Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays can be both unsorted or sorted. It may be assumed that elements in both array are distinct.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an two integers m and n denoting the size of arr1 and arr2 respectively. The following two lines contains the space separated elements of arr1 and arr2 respectively.

Output:
Print "Yes"(without quotes) if arr2 is subset of arr1.
Print "No"(without quotes) if arr2 is not subset of arr1.

Constraints:
1<=T<=10^5
1<=m,n<=10^5
1<=arr1[i],arr2[j]<=10^5

Example:
Input:

3
6 4
11 1 13 21 3 7
11 3 7 1
6 3
1 2 3 4 5 6
1 2 4
5 3
10 5 2 23 19
19 5 3

Output:
Yes
Yes
No
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;


class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
   int n1=sc.nextInt();
   int n2=sc.nextInt();
   int []a1= new int[n1];
   int []a2= new int[n2];
   for(int a1_i=0;a1_i<n1;a1_i++){
       a1[a1_i]=sc.nextInt();
   }
   for(int a2_i=0;a2_i<n2;a2_i++){
       a2[a2_i]=sc.nextInt();
   }
   if(isSubset(a1,a2,n1,n2)){System.out.println("Yes");}
   else{System.out.println("No");}
}
}
public static boolean isSubset(int[]a1, int[]a2,int n1,int n2 ){
   HashSet<Integer> hset= new HashSet<>();
   for(int i=0;i<n1;i++){
       if(!hset.contains(a1[i])){
           hset.add(a1[i]);
       }
   }
   for(int j=0;j<n2;j++){
       if(!hset.contains(a2[j])){
           return false;
       }
   }
   return true;
}
}
EXECUTION TIME: 0.12s


b) Company: Rockstand

Question:
Given two arrays with distinct elements A and B, print intersection (or common elements) of the two arrays.  If no element is common in two arrays, then print Zero.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and M,N is the size of array A and M is size of array B.
The second line of each test case contains N input A[i].
The third line of each test case contains M input B[i].

Output:
Print the intersecting elements sorted in ascending order .If no element is common in two array, then print "Zero" without quotes.

Constraints:
1 ≤ T ≤ 50
1 ≤ N, M ≤ 100
1 ≤ A[i], B[i] ≤ 1000

Example:
Input:2
5 3
89 24 75 11 23
89 2 4
6 5
1 2 3 4 5 6
3 4 5 6 7
Output:
89
3 4 5 6
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
public static void main (String[] args) {
 Scanner sc = new Scanner (System.in);
 int m = sc.nextInt();
 for(int mi=0;mi<m;mi++){
     int n1=sc.nextInt();
     int n2=sc.nextInt();
     int []a1= new int[n1];
     int []a2= new int[n2];
     for(int a1_i=0;a1_i<n1;a1_i++){
         a1[a1_i]=sc.nextInt();
     }
     for(int a2_i=0;a2_i<n2;a2_i++){
         a2[a2_i]=sc.nextInt();
     }
     isSubset(a1,a2,n1,n2);
 }
 }
 public static void isSubset(int[]a1, int[]a2,int n1,int n2 ){
     HashSet<Integer> hset= new HashSet<>();
     List<Integer>list=new ArrayList<>();
     for(int i=0;i<n1;i++){
         if(!hset.contains(a1[i])){
             hset.add(a1[i]);
         }
     }
     for(int j=0;j<n2;j++){
         if(hset.contains(a2[j])){
             list.add(a2[j]);
         }
     }
     
     Collections.sort(list);
     if(list.size()==0){System.out.print("Zero");}
     else{
     for(int num:list){
         System.out.print(num+" ");
     }
     }
     System.out.println();
 }
}
EXECUTION TIME:0.16s

Comments

Popular Posts

TARUNKUMAR RAWAT:Fourier Series and Fourier Transform

Tarunkumar Rawat Fourier Series: Click here to download Tarun Rawat Fourier Series Tarunkumar Rawat Fourier Transform: Click here to download TarunRawat Fourier Transform Steps to follow: 1) Click on the above link 2) Wait for 3 secs and click on the link 3) Wait for 5 secs and click on skip ad NOTE: We do require these ads to fund the site and provide books for free. Besides these are harmless and just require 5 secs waiting and nothing else. Please be patient for 5 secs and get your desired book for FREE!  IMPORTANT: We believe that every author deserves some respect and the same should be shown towards them by purchasing the books and lauding the efforts of author. Hence we recommend to buy the book. You can buy the book for an affordable rate in given below link: Using of PDF will be at your own risk and EXTC RESOURCES IS NOT RESPONSIBLE for any consequences of the same. We provide links for book and donot endorse piracy.  

Modern Digital and Analog Communication (BP Lathi,3rd ed)

Another Analog communication book: Modern Digital and Analog Communication (BP Lathi,3rd ed) : Click here to download Modern Digital and Analog Communication (BP Lathi,3rd ed) Copyright Disclaimer: This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.

Numerical methods with MATLAB

For Numerical methods along with MATLAB the best book is: Chapra Applied Numerical Methods MATLAB Engineers Scientists 3rd edition : Click here to download Steven Chapra with MATLAB For altenate link : Click here to download Steen Chapra with MATLAB