-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFourSumCount.java
More file actions
37 lines (29 loc) · 1009 Bytes
/
FourSumCount.java
File metadata and controls
37 lines (29 loc) · 1009 Bytes
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
package com.company;
import java.util.HashMap;
import java.util.Map;
public class FourSumCount {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
// key: sum, value: frequency of sum.
int n = A.length;
int result = 0;
// Get all possible two-sums between C and D.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = C[i] + D[j];
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
// Count the number of two-sums
// between A and B that equals to
// opposite of any two-sum between C and D
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = A[i] + B[j];
if (map.containsKey(-sum))
result += map.get(-sum);
}
}
return result;
}
}