Editor

Zero Sum Across Four Lists

#171 · Dictionaries and sets · Expert · 190 XP · about 25 min

Write count_zero_quads(a, b, c, d) that counts the ways to pick one position from each of the four lists so that the four values add up to zero: the number of tuples (i, j, k, l) with a[i] + b[j] + c[k] + d[l] == 0.

Positions are what count, so equal values at different positions make different tuples.

Examples

count_zero_quads([1, 2], [-2, -1], [-1, 2], [0, 2])  # returns 2
count_zero_quads([0], [0], [0], [0])                 # returns 1
count_zero_quads([1], [1], [1], [1])                 # returns 0

In the first example the two tuples are 1 + (-2) + (-1) + 2 and 2 + (-1) + (-1) + 0.

Constraints

  • All four lists have the same length n, with 0 <= n <= 200
  • Values are between -2**28 and 2**28

Note

With n = 200 there are 1.6 billion tuples, far too many to try one by one.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.