I am creating two list having sum of pairs of two distinct array in O(n^2) tc. And thereafter I use HashMap for solving it and in worst case it will take O(n^2) tc. Where in comments people say O(n^2 * log n) has passed.
map= new HashMap<>();
for(int i=0; i<len; i++){
if(map.containsKey(sum12[i])){
map.put(sum12[i], map.get(sum12[i]) + 1);
}
else{
map.put(sum12[i], 1);
}
}
map1= new HashMap<>();
for(int i=0; i<len; i++){
if(map1.containsKey(sum34[i])){
map1.put(sum34[i], map1.get(sum34[i]) + 1);
}
else{
map1.put(sum34[i], 1);
}
}
long count= 0;
int key, val;
for(Map.Entry<Integer, Integer> entry : map1.entrySet()){
key= entry.getKey();
val= entry.getValue();
if(map.containsKey(0-key)){
count += (val * (map.get(0-key)));
}
}