A row of n numbered support brackets sits along a straight steel beam, each bracket exerting a load on the beam (a negative value means the bracket pulls upward rather than pressing down). Find the leftmost bracket position i (0-indexed along the row) such that the total load of every bracket strictly to the left of position i equals the total load of every bracket strictly to the right of position i; the bracket at position i itself is excluded from both totals, and an empty side counts as a total of 0. Print that position, or print -1 if no such balanced position exists anywhere in the row.
n.n space-separated integers, the load at each bracket in order (each may be negative, zero, or positive).Print a single integer: the smallest index i (0-based) at which the beam is balanced, or -1 if no such index exists.
Example 1
Input
5 2 3 -1 8 4
Expected
3
Explanation
Loads are [2,3,-1,8,4]. At index 3 (the bracket carrying load 8), the left total is 2+3-1=4 and the right total is 4, which are equal. No earlier index (0,1,2) has matching left/right totals, so 3 is the leftmost balanced position.
Example 2
Input
3 1 -1 4
Expected
2
Explanation
Loads are [1,-1,4]. At index 2 (the last bracket), the left total is 1-1=0 and the right total is 0 (nothing remains to the right), which are equal, and no earlier index balances, so the answer is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →