weights of length n. A **cut** after the first c elements (1 ≤ c ≤ n − 1) is balanced if the sum of those first c elements equals the sum of the rest.
Before counting, you may replace **at most one** element of the array with the number spare (or change nothing).
Return the largest possible number of balanced cuts.weights=[3, 1, 2, 4], spare=6 → 1
Without a change the cuts give 3|7, 4|6, 6|4, none balanced. Replacing 4 with 6 gives [3, 1, 2, 6], and the cut 3 + 1 + 2 | 6 balances 6 = 6. No change yields more than one balanced cut.
weights=[2, -2, 2, -2, 0], spare=9 → 2
The array sums to 0. The cuts after the second and after the fourth element give 0 = 0, that is 2, and no replacement does better.
weights=[5, 5], spare=1 → 1
The only cut, 5 | 5, is already balanced.
weights=[3, 1, 2, 4], spare=6
1
Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.