09 Sept 2026Go / Python / TypeScriptEasy

Find Pivot Index

Find the leftmost index whose left and right element sums are equal.

Keep a running left sum and derive the right sum from the array total at each position.

complexity

O(n) time and O(1) extra space.

solution files

  • Go find-pivot-index/solution.go
  • Python find-pivot-index/solution.py
  • TypeScript find-pivot-index/solution.ts

Solution files

Gofind-pivot-index/solution.go
package main

func pivotIndex(nums []int) int {
	total := 0
	for _, value := range nums {
		total += value
	}
	left := 0
	for index, value := range nums {
		if left == total-left-value {
			return index
		}
		left += value
	}
	return -1
}
Pythonfind-pivot-index/solution.py
class Solution:
    def pivotIndex(self, nums: list[int]) -> int:
        total, left = sum(nums), class="syntax-number">0
        for index, value in enumerate(nums):
            if left == total - left - value: return index
            left += value
        return -class="syntax-number">1
TypeScriptfind-pivot-index/solution.ts
function pivotIndex(nums: number[]): number {
  const total = nums.reduce((sum, value) => sum + value, class="syntax-number">0);
  let left = class="syntax-number">0;
  for (let index = class="syntax-number">0; index < nums.length; index += class="syntax-number">1) {
    if (left === total - left - nums[index]) return index;
    left += nums[index];
  }
  return -class="syntax-number">1;
}