β Find element in 2D array (Contest)
Find element in 2D array easy Time Limit: 2 sec Memory Limit: 128000 kB
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int q = sc.nextInt();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
int num = sc.nextInt();
set.add(num);
}
}
while (q-- > 0){
int x = sc.nextInt();
if (set.contains(x)){
System.out.println("Yes");
} else {
System.out.println("No");
}
}
sc.close();
}
}Last updated