-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBerSU Ball
More file actions
37 lines (30 loc) · 1.06 KB
/
BerSU Ball
File metadata and controls
37 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
int[] boys = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) boys[i] = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(br.readLine().trim());
int[] girls = new int[m];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < m; i++) girls[i] = Integer.parseInt(st.nextToken());
Arrays.sort(boys);
Arrays.sort(girls);
int i = 0, j = 0, pairs = 0;
while (i < n && j < m) {
if (Math.abs(boys[i] - girls[j]) <= 1) {
pairs++;
i++;
j++;
} else if (boys[i] < girls[j]) {
i++;
} else {
j++;
}
}
System.out.print(pairs);
}
}