a)Company: Qualcomm, GE
Question:
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
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:
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:
Constraints:
1 ≤ T ≤ 50
1 ≤ N, M ≤ 100
1 ≤ A[i], B[i] ≤ 1000
Example:
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
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
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
Post a Comment