454. 4Sum II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the number of tuples (i, j, k, l) from four arrays nums1, nums2, nums3, nums4 such that nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0. The arrays all have the same length n.
Solution
4Sum II in Python
class Solution:
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
# Use a hash map (dictionary) to store sums of pairs from nums1 and nums2
# and their frequencies. collections.Counter is a specialized dictionary for this.
# Phase 1: Populate the map. Time: O(n^2)
sum_map = collections.Counter(a + b for a in nums1 for b in nums2)
count = 0
# Phase 2: Iterate through nums3 and nums4 to find complements.
# The equation is a + b + c + d = 0, rearranged to a + b = -(c + d).
# Time: O(n^2)
for c in nums3:
for d in nums4:
target = -(c + d)
# Add the number of pairs (a, b) that sum to the target.
# .get(key, 0) is used to handle cases where the target is not in the map.
count += sum_map.get(target, 0)
return count
4Sum II in Java
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
// Use a hash map to store the sums of pairs from nums1 and nums2 and their frequencies.
// Key: sum, Value: frequency.
Map<Integer, Integer> sumMap = new HashMap<>();
// Phase 1: Populate the map with sums from nums1 and nums2.
// Time complexity for this part is O(n^2).
for (int n1 : nums1) {
for (int n2 : nums2) {
int sum = n1 + n2;
sumMap.put(sum, sumMap.getOrDefault(sum, 0) + 1);
}
}
int count = 0;
// Phase 2: Iterate through nums3 and nums4 to find complements.
// The equation is a + b + c + d = 0, which can be rearranged to a + b = -(c + d).
// Time complexity for this part is also O(n^2).
for (int n3 : nums3) {
for (int n4 : nums4) {
int target = -(n3 + n4);
// If the required complement exists in the map, add its frequency to the total count.
count += sumMap.getOrDefault(target, 0);
}
}
return count;
}
}
4Sum II in C++
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
// Use a hash map to store the sums of pairs from nums1 and nums2 and their frequencies.
// Key: sum, Value: frequency.
unordered_map<int, int> sumMap;
// Phase 1: Populate the map with sums from nums1 and nums2.
// Time complexity for this part is O(n^2).
for (int n1 : nums1) {
for (int n2 : nums2) {
sumMap[n1 + n2]++;
}
}
int count = 0;
// Phase 2: Iterate through nums3 and nums4 to find complements.
// The equation is a + b + c + d = 0, which can be rearranged to a + b = -(c + d).
// Time complexity for this part is also O(n^2).
for (int n3 : nums3) {
for (int n4 : nums4) {
int target = -(n3 + n4);
// Check if the required complement exists in the map.
if (sumMap.count(target)) {
// If it exists, add its frequency to the total count.
count += sumMap[target];
}
}
}
return count;
}
};
4Sum II in TypeScript
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
// Use a Map to store the sums of pairs from nums1 and nums2 and their frequencies.
// Key: sum, Value: frequency.
const sumMap = new Map<number, number>();
// Phase 1: Populate the map with sums from nums1 and nums2.
// Time complexity for this part is O(n^2).
for (const n1 of nums1) {
for (const n2 of nums2) {
const sum = n1 + n2;
sumMap.set(sum, (sumMap.get(sum) || 0) + 1);
}
}
let count = 0;
// Phase 2: Iterate through nums3 and nums4 to find complements.
// The equation is a + b + c + d = 0, which can be rearranged to a + b = -(c + d).
// Time complexity for this part is also O(n^2).
for (const n3 of nums3) {
for (const n4 of nums4) {
const target = -(n3 + n4);
// If the required complement exists in the map, add its frequency to the total count.
if (sumMap.has(target)) {
count += sumMap.get(target)!;
}
}
}
return count;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.