Loading...
Loading...
Loading...
Four lists a, b, c and d each have n integers. Count the ways to pick one item from each list, as index choices (i, j, k, l), so that a[i] + b[j] + c[k] + d[l] = 0.
Example
Input
2 1 2 -2 -1 -1 2 0 2
Output
2
Two choices add up to zero.
Try the problem first. Open one hint at a time.
Four nested loops take n^4 steps, which is too slow.
Count all sums a[i] + b[j] in a Counter, then look up -(c[k] + d[l]) for each pair from c and d.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor