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 (0,0,0,1): 1 + (-2) + (-1) + 2 = 0 and (1,1,0,0): 2 + (-1) + (-1) + 0 = 0. No other combination sums to 0.
Example 2
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output: 1
Explanation: There is exactly one tuple (0,0,0,0), and 0 + 0 + 0 + 0 = 0, so the count is 1.
Example 3
Input: nums1 = [1,1], nums2 = [1,1], nums3 = [1,1], nums4 = [1,1]
Output: 0
Explanation: Every choice adds 1 + 1 + 1 + 1 = 4, which is never 0, so no tuple qualifies.
Split the four arrays into two halves. Count every pairwise sum from the first two arrays in a hash map, then for each pairwise sum of the last two arrays look up how many first-half sums cancel it (equal its negative). This turns an O(n^4) search into O(n^2).
Run checks all cases above. Submit evaluates all test cases.