Quant Memo
Coding/●●●●

Count the pairs that fill a block

Resting child orders have sizes (some can be negative, representing sells). You want to know how many pairs of them combine to exactly the block size.

sizes = [1, 5, 7, -1, 5], target = 6
-> 3          # (1,5), (1,5) using the other 5, and (7,-1)

Return the number of unordered index pairs (i,j)(i, j) with i<ji < j and sizes[i] + sizes[j] == target. Aim for O(n)O(n).

Show a hint

For each new element, how many earlier elements are exactly the complement it needs? A running count of values answers that in O(1)O(1).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions