Back to Problem
Official
671 views
Editorial: Find the Balance Index
by admin ·
4
1 Intuition
If you know the total, the right sum at index i is total - left - nums[i].
2 Approach
Compute the total once, then scan left to right maintaining the left sum and check the balance condition at each index.
3 Complexity Analysis
Time O(n), Space O(1).
4 Solution Code
class Solution:
def pivotIndex(self, nums: list[int]) -> int:
total, left = sum(nums), 0
for i, x in enumerate(nums):
if left == total - left - x:
return i
left += x
return -1
5 Code Walkthrough
The reference solution below implements the approach step by step.