Files
framework_note/leetcode/note/easy/724_find_pivot_index.md
T
2026-06-05 18:15:42 -04:00

3.4 KiB

id, title, difficulty, tags, status, date_solved, leetcode_url, review_needed
id title difficulty tags status date_solved leetcode_url review_needed
724 Find Pivot Index Easy
array
prefix-sum
Solved 2026-06-05 https://leetcode.com/problems/find-pivot-index/ false

724. Find Pivot Index

[!info] Problem Link: LeetCode - Find Pivot Index

📝 Problem Description

Given an array of integers nums, calculate the pivot index of this array.

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the right of the index.

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

Return the leftmost pivot index. If no such index exists, return -1.


📥 Example 1

Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11

📥 Example 2

Input: nums = [1,2,3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement.

📥 Example 3

Input: nums = [2,1,-1] Output: 0 Explanation: The pivot index is 0. Left sum = 0 (no elements to the left of index 0) Right sum = nums[1] + nums[2] = 1 + (-1) = 0


💡 Approaches & Explanations

Approach 1: Prefix Sum — Optimal

The key insight is that for any index i, we can determine the right_sum if we know the total_sum and the left_sum. Specifically: right_sum = total_sum - left_sum - nums[i]. The condition for i being a pivot index is left_sum == right_sum, which simplifies to: left_sum == total_sum - left_sum - nums[i] or 2 * left_sum + nums[i] == total_sum.

  1. Calculate the total_sum of the array.
  2. Initialize left_sum = 0.
  3. Iterate through the array. At each index i:
    • Check if the condition left_sum == total_sum - left_sum - nums[i] holds.
    • If yes, return i.
    • Update left_sum += nums[i].
  4. If the loop finishes without returning, return -1.

📊 Complexity Analysis

  • Time Complexity: \mathcal{O}(N) where N is the number of elements in nums. We traverse the array twice (once for the total sum, once for the search).
  • Space Complexity: \mathcal{O}(1) as we only use a few variables for sums.

💻 Code Implementations

Python3

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        total_sum = sum(nums)
        left_sum = 0
        
        for i, num in enumerate(nums):
            if left_sum == (total_sum - left_sum - num):
                return i
            left_sum += num
            
        return -1

🧠 Key Takeaways & Lessons

  • Equation Simplification: Many "equilibrium" or "pivot" problems can be solved by expressing the "right side" in terms of the "total" and the "left side," reducing the need for multiple passes or extra space.
  • Prefix Sum Pattern: This is a classic application of the prefix sum concept, where we maintain a running total to answer queries or check conditions in \mathcal{O}(1) time per element.
  • Handling Edge Cases: The problem explicitly defines sum at edges as 0, which is naturally handled by starting left_sum at 0 and checking the condition before updating it.