You're given four integer arrays nums1, nums2, nums3, and nums4, each containing n integers. Your mission is to find how many different ways you can pick one number from each array such that their sum equals zero.
More formally, return the number of tuples (i, j, k, l) where:
0 ≤ i, j, k, l < nnums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example:
If nums1 = [1, 2], nums2 = [-2, -1], nums3 = [-1, 2], nums4 = [0, 2]
We can form: 1 + (-2) + (-1) + 2 = 0 and 2 + (-1) + (-1) + 0 = 0
So the answer is 2.
This problem tests your ability to optimize from a naive O(n⁴) solution to an efficient O(n²) approach using hash tables and the "meet in the middle" technique.
Input & Output
Visualization
Time & Space Complexity
Two passes of O(n²) each - first to populate hash table, second to find complements
Hash table can store up to n² different sums from first two arrays
Constraints
- n == nums1.length == nums2.length == nums3.length == nums4.length
- 1 ≤ n ≤ 200
- -228 ≤ nums1[i], nums2[i], nums3[i], nums4[i] ≤ 228
- All arrays have the same length
- Large integer values require careful overflow handling