A designer wants to split a toolbar so the items left of a chosen item are exactly as wide in total as the items to its right. The item widths are in widths.
Write balancePoint(widths) that returns the smallest index i where the sum of widths[0..i - 1] equals the sum of widths[i + 1..]. The chosen item itself is on neither side, and an empty side sums to 0. Return -1 if no index works.
Input: widths = [1,4,2,3,2] Output: 2
Left of index 2 is 1 + 4 = 5; right of it is 3 + 2 = 5.
Input: widths = [1,2,3] Output: -1
No index has equal sums on both sides.
0 <= widths.length <= 10^5-1000 <= widths[i] <= 1000Run 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).
I'm here to help with #39 Balance Point of a Toolbar. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.