4Sum II
Given four integer arrays nums1, nums2, nums3, nums4, each of length n, count the number of index tuples (i, j, k, l) such that nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0.
Open official problem prompt ↗Count all four-index combinations, one index per array, whose four values sum to zero.
Split four dice into two pairs. Write down how many ways each total appears for the first pair, then for every total of the second pair look up how many first-pair totals cancel it out.
- Input
- nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
- Output
- 2
- Why
- The two valid tuples are (0,0,0,1): 1 + (-2) + (-1) + 2 = 0, and (1,1,0,0): 2 + (-1) + (-1) + 0 = 0.
n == nums1.length == nums2.length == nums3.length == nums4.length1 <= n <= 200-2^28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^28