Skip to content

Balance Point Index

Easy#prefix-sums#running-sum~15 min

The mission

Problem

Given a list of whole numbers, which may be negative, find a position where the values strictly to its left add up to the same total as the values strictly to its right. An empty side counts as zero. Return the leftmost such index, or -1 if there is none.

Examples

Input:  values = [2, 7, 1, 5, 4]
Output: 2
Why:    2 + 7 = 9 on the left of index 2, and 5 + 4 = 9 on the right
Input:  values = [1, 2, 3]
Output: -1
Why:    no index splits the list into equal sides
Input:  values = [5]
Output: 0
Why:    edge case, both sides of the only value are empty and sum to 0

Hints

0 / 3

Target time
O(n)
Target space
O(1)

Constraints

  • values may be negative

Stuck on the idea rather than the code? O(1) and O(n) covers it.

Python 3Ctrl+Enter runs
Custom test case