-
Notifications
You must be signed in to change notification settings - Fork 30
/
4Sum II.cpp
39 lines (27 loc) · 875 Bytes
/
4Sum II.cpp
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
38
39
/*
Solution by Rahul Surana
***********************************************************
Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int,int> m;
for(auto x: nums1){
for(auto z: nums2){
m[x+z]++;
}
}
int ans = 0;
for(auto x: nums3){
for(auto z: nums4){
ans += m[-(x+z)];
}
}
return ans;
}
};