#724EasyPrefix Sum

Find Pivot Index

Use total sum and running left sum to find where left equals right.

GoogleAmazonBloomberg

Problem Statement

Given an array nums, find the pivot index: the index where the sum of all numbers to the left equals the sum to the right. If no such index exists, return -1. If there are multiple, return the leftmost.

Examples

Example 1

Input:nums = [1,7,3,6,5,6]
Output:3

Explanation: Left sum = 1+7+3=11. Right sum = 5+6=11.

Example 2

Input:nums = [1,2,3]
Output:-1

Explanation: No pivot exists.

Constraints

  • 1 ≤ nums.length ≤ 10⁴
  • -1000 ≤ nums[i] ≤ 1000

Solutions

1
Brute Force — Recompute sums
TimeO(n²)SpaceO(1)

For each index, compute left sum and right sum separately. O(n²) due to repeated summations.

Visual Animation
def pivotIndex(nums: list[int]) -> int:
    for i in range(len(nums)):
        if sum(nums[:i]) == sum(nums[i+1:]):
            return i
    return -1

Related Concepts

Deepen your understanding with these related topics from our AI Glossary:

Deepen your understanding

Want to master the core concepts?

Our free AI Glossary covers 190+ topics — from Prefix Sum to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.

Browse AI Glossary All Problems
39+
AI Models
₹69
Per day used
4
Languages

Stuck? Ask AI to explain it step by step.

Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.

Free to start · No credit card required to explore

Get Started Free
Back to all problems