Loading
Loading
A librarian lines up shelves in a row, each holding some number of books. She wants to find a shelf she can stand at such that the total number of books strictly to her left equals the total strictly to her right.
Return the leftmost index of such a shelf, or -1 if no shelf works. The shelf she stands at is counted on neither side.
books = [1, 7, 3, 6, 5, 6]index 0left = 0, right = 27 — noindex 1left = 1, right = 20 — noindex 2left = 8, right = 17 — noindex 3left = 11, right = 11 — balanced3Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Left of index 3 sums to 11, right of index 3 sums to 11.
Example 2
No shelf splits the rest into two equal halves.
Example 3
At index 0 the left side is empty (0) and the right side is 1 + (-1) = 0.