beginner · prefix sums · arrays
Where do both sides weigh the same?
Problem
Find the first position i such that the sum of the values left of i equals the sum of the values right of i (the value at i belongs to neither side). Return −1 if there is none.
Constraints
- 0 ≤ n ≤ 32
- values from −99 to 99
Examples
[1, 7, 3, 6, 5, 6] → 3 — 1 + 7 + 3 = 11 = 5 + 6.
[2, 1, -1] → 0 — Left is empty (0), right is 1 − 1 = 0.
Show the solution
Total and running left sum
With the total known, the right side of i is total − left − a[i]; keep left as a running sum.
Why it is correct
Before position i is checked, left is the sum of a[0..i), so the comparison tests exactly the definition. The first match is returned.
Complexity
Time O(n), extra space O(1). Two passes: one for the total, one for the check.
Edge cases
- Position 0 has an empty left side (0).
- The last position has an empty right side.
- Empty array: −1.
Common mistakes
- Including a[i] in one of the sides.
- Updating left before the check.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class PivotIndex
{
/// <summary>The first index whose left sum equals its right sum, or -1. O(n), O(1) space.</summary>
public static int Find(int[] values)
{
int total = values.Sum();
int left = 0;
for (int i = 0; i < values.Length; i++)
{
if (left == total - left - values[i]) // right side = total - left - values[i]
return i;
left += values[i];
}
return -1;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/PivotIndex.cs
from collections.abc import Sequence
def find(values: Sequence[int]) -> int:
"""The first index whose left sum equals its right sum, or -1. O(n), O(1) space."""
total = sum(values)
left = 0
for i, value in enumerate(values):
if left == total - left - value: # right side = total - left - value
return i
left += value
return -1
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/pivot_index.py
package dev.bekadan.dsa.problems;
import java.util.Arrays;
public final class PivotIndex {
private PivotIndex() {
}
/** The first index whose left sum equals its right sum, or -1. O(n), O(1) space. */
public static int find(int[] values) {
int total = Arrays.stream(values).sum();
int left = 0;
for (int i = 0; i < values.length; i++) {
if (left == total - left - values[i]) { // right side = total - left - values[i]
return i;
}
left += values[i];
}
return -1;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/PivotIndex.java
#pragma once
#include <cstddef>
#include <numeric>
#include <span>
namespace bekadan::dsa::problems {
// The first index whose left sum equals its right sum, or -1. O(n), O(1) space.
inline std::ptrdiff_t pivot_index(std::span<const int> values) {
const int total = std::accumulate(values.begin(), values.end(), 0);
int left = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (left == total - left - values[i]) // right side = total - left - values[i]
return static_cast<std::ptrdiff_t>(i);
left += values[i];
}
return -1;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/pivot_index.hpp