4Sum II
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int n = A.length;
int result = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int a : A){
for(int b : B){
int sumAB = a + b;
map.put(sumAB, map.getOrDefault(sumAB, 0) + 1);
}
}
for(int c : C){
for(int d : D){
int sumCD = -(c + d);
if(map.containsKey(sumCD)){
result += map.get(sumCD);
}
}
}
return result;
}
}Last updated