Find Pivot Index in Python – LeetCode75 Explained

The Find Pivot Index problem from LeetCode 75 is a classic coding interview question that tests your understanding of prefix sums and array balancing. This problem is often used to evaluate how well candidates can manage cumulative sums and optimize linear scans.

Find Pivot Index in Python – LeetCode75 Explained

In this blog, we’ll explore the problem statement, go through examples, and implement a clean Python solution step by step.

Problem Statement

Given an integer array nums, return 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 no such index exists, return -1. If there are multiple pivot indices, return the leftmost one.

Understanding the Problem

  • The pivot index splits the array into two balanced parts.
  • Instead of recomputing sums for each index (which would be inefficient), we can use prefix sums.
  • Key formula:
left_sum == total_sum - left_sum - nums[i]

Where:

  • left_sum = sum of elements before index i
  • total_sum - left_sum - nums[i] = sum of elements after index i

Examples

Example 1:

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

Explanation:  
Left sum at index 3 = 1 + 7 + 3 = 11  
Right sum at index 3 = 5 + 6 = 11  
Pivot index = 3

Example 2:

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

Explanation:  
No index balances the array.

Example 3:

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

Explanation:  
Left sum = 0, Right sum = 1 + (-1) = 0  
Pivot index = 0

Python Solution – Step by Step

Here’s the clean Python implementation:

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        total_sum = sum(nums)
        left_sum = 0

        for i, num in enumerate(nums):
            # Step 3: Check if left sum equals right sum
            if left_sum == total_sum - left_sum - num:
                return i
            # Step 2: Update left sum
            left_sum += num

        return -1

Step 1: Compute Total Sum

total_sum = sum(nums)

We calculate the total sum of the array once.

Step 2: Traverse with Running Left Sum

left_sum = 0

We keep a running total of elements before the current index.

Step 3: Check Pivot Condition

if left_sum == total_sum - left_sum - num:
    return i

If left sum equals right sum, return the current index.

Step 4: Return Result

return -1

If no pivot index is found, return -1.

Why This Solution Works

  • Instead of recalculating sums for every index (O(n²)), we use the total sum and a running left sum to check the condition in O(1) time for each index.
  • This reduces the complexity to O(n).

Time and Space Complexity

  • Time Complexity: O(n) – single pass through the array.
  • Space Complexity: O(1) – only two variables used (left_sum and total_sum).

Edge Cases to Consider

InputOutputExplanation
nums = [0]0Single element array → pivot index is 0
nums = [1, -1, 0]2Left sum = 0, Right sum = 0
nums = [1,2,3]-1No pivot index exists
nums = [2,1,-1]0First index works as pivot

Real-World Applications

  • Financial analysis: Finding equilibrium points in profit/loss arrays.
  • Load balancing: Splitting tasks into two equal halves.
  • Game development: Identifying balance points in scoring systems.
  • Data analytics: Detecting symmetry in datasets.

Conclusion

The Find Pivot Index problem is an elegant example of how prefix sums simplify array balancing problems. With a single pass and minimal extra space, we can efficiently find the equilibrium index.

Mastering this concept builds strong fundamentals for array and prefix sum problems, which are frequently asked in coding interviews.

2 thoughts on “Find Pivot Index in Python – LeetCode75 Explained”

Leave a Comment