Skip to content
BytePatterns

Four List Zero Tuples

MediumHash Tables#hash-map#meet-in-the-middle~30m

Problem

Four lists of equal length are given. Count the ways to pick exactly one value from each list so the four values add up to zero. Two picks count separately whenever they come from different positions, even if the values happen to be equal.

Examples

Input:  a = [1, 2], b = [-2, -1], c = [-1, 2], d = [0, 2]
Output: 2
Why:    1 + (-2) + (-1) + 2 and 2 + (-1) + (-1) + 0 both reach zero
Input:  a = [0], b = [0], c = [0], d = [0]
Output: 1
Why:    there is only one pick to make and it works
Input:  a = [1], b = [1], c = [1], d = [1]
Output: 0
Why:    edge case, no combination can reach zero

Hints

0 / 3

Stuck on the idea rather than the code? Two Sum covers it.