Balance Point Count in a Weighted Sensor Array
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests the ability to use prefix sums to efficiently count indices where the weighted sum to the left equals the weighted sum to the right.
Balance Point Count in a Weighted Sensor Array
A sensor array records integer readings. Given the readings and a positive integer weight w, define the weighted left sum at index i as:
w * (readings[0] + readings[1] + ... + readings[i-1])
and the weighted right sum at index i as:
readings[i+1] + readings[i+2] + ... + readings[n-1]
Note that the left side is multiplied by w but the right side is not.
Index i is called a balance point if its weighted left sum equals its weighted right sum.
For i = 0, the weighted left sum is 0 (empty product).
For i = n-1, the weighted right sum is 0 (empty sum).
Return the count of balance points across all indices.
Constraints
1 <= len(readings) <= 2000-10^4 <= readings[i] <= 10^41 <= w <= 100- An array of length 1 always has exactly 1 balance point (index 0).
Examples
Example 1
Input: count_balance_points([2, 3, 1, 6, 2], 2)
Output: 0
Example 2
Input: count_balance_points([1, -1, 1, -1, 1], 1)
Output: 5
Example 3
Input: count_balance_points([5], 7)
Output: 1
Hints
Hint 1
Compute the total sum of the array first; then think about how you can express the right sum at index i in terms of a running prefix sum you maintain as you scan left to right.
Hint 2
At index i, right_sum = total_sum - prefix_sum - readings[i], where prefix_sum is the sum of all elements before i. The balance condition becomes w * prefix_sum == right_sum.
Hint 3
No extra data structure beyond a running sum is needed — the check is O(1) per index, giving an O(n) overall solution.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
For each index i, the balance condition is:
w * prefix_sum(i) == total_sum - prefix_sum(i) - readings[i]
where prefix_sum(i) = readings[0] + ... + readings[i-1].
This means we only need two things computed before the loop:
total = sum(readings)— one O(n) pass.- A running
prefixvariable updated incrementally as we scan.
At every index i we check the condition in O(1), increment the counter if it holds, then add readings[i] to prefix.
Why It Is Correct
- The right sum at
iistotal - prefix - readings[i]by the definition of a total sum and the running prefix. - The left sum is
w * prefixby the problem definition. - Checking equality and summing over all valid indices directly counts balance points.
- Edge case
n = 1:prefix = 0, right sum =total - 0 - readings[0] = 0, condition becomes0 == 0→ always counted. ✓ - Negative values and
w > 1are handled algebraically with no special cases.
Complexity
| Time | O(n) — single pass after one O(n) sum |
| Space | O(1) — only scalar variables |
Reference Solution
def count_balance_points(readings: list, w: int) -> int:
total = sum(readings)
prefix = 0
count = 0
for i, val in enumerate(readings):
# balance condition: w * prefix == total - prefix - val
if w * prefix == total - prefix - val:
count += 1
prefix += val
return count