Skip to the code
+25 XP

#31 Four Panel Zero Balance

medium

A finance dashboard has four panels of adjustments, a, b, c, and d. Picking one adjustment from each panel balances the books if the four add up to 0.

Write zeroBalanceCount(a, b, c, d) that returns how many index tuples (i, j, x, y) have a[i] + b[j] + c[x] + d[y] === 0.

Example 1

Input: a = [1,-1], b = [0,2], c = [-2,1], d = [1,0]
Output: 3

For example 1 + 0 + (-2) + 1 = 0. Three tuples work in total.

Example 2

Input: a = [0], b = [0], c = [0], d = [0]
Output: 1

The only tuple adds up to 0.

Constraints

  • 0 <= a.length, b.length, c.length, d.length <= 1000
  • -2^28 <= values <= 2^28
  • The answer is at most 2^31 - 1.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).