Skip to content

Instantly share code, notes, and snippets.

@gauravkr0071
Created August 14, 2021 20:49
Show Gist options
  • Save gauravkr0071/a84c199f7b053a26cfe2cd9d8bb5d7ec to your computer and use it in GitHub Desktop.
Save gauravkr0071/a84c199f7b053a26cfe2cd9d8bb5d7ec to your computer and use it in GitHub Desktop.
4 Sum II
/*
454. 4Sum II
Medium
2280
85
Add to List
Share
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
Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output: 2
Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output: 1
Constraints:
n == nums1.length
n == nums2.length
n == nums3.length
n == nums4.length
1 <= n <= 200
-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228
*/
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> abSum;
for(auto a : A) {
for(auto b : B) {
++abSum[a+b];
}
}
int count = 0;
for(auto c : C) {
for(auto d : D) {
auto it = abSum.find(0 - c - d);
if(it != abSum.end()) {
count += it->second;
}
}
}
return count;
}
//https://leetcode.com/problems/4sum-ii/discuss/93925/Concise-C%2B%2B-11-code-beat-99.5 READ THE COMMENTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment