Skip to main content
  1. LeetCode/

LeetCode 724: Find Pivot Index

·2 mins· ·
LeetCode Blog Daily Easy Prefix Sum
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-04-19

  • Total time: 20:00.00

Intuition
#

Initially, I used two prefix sum arrays — one from left to right, and one from right to left.

Then, I iterated from the left index to the end, and if the sum from the left prefix array matched the sum from the right prefix array, I returned that index.

However, I realized that we don’t actually need two prefix sum arrays. We only need the prefix sum from right to left.

For the left-to-right part, we can simply maintain a running total using a constant variable to store the sum of the left-side numbers.

Approach
#

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        n = len(nums)

        ps = [0] * (n + 1)

        for i in range(n-1, -1, -1):
            ps[i] = ps[i+1] + nums[i]
        
        
        count = 0
        for i in range(0, n):
            if ps[i+1] == count:
                return i
            count += nums[i]

        return -1

Findings
#

NA

Encountered Problems
#

Array indexing can be tricky when working with prefix sums.

When iterating from left to right, we have to use ps[i + 1] to check the cumulative sum. This is because the first element in the ps array represents the sum of zero elements (i.e., an empty prefix).

As a result, if we want to compare the sum corresponding to index 0 in the original array, we need to use ps[1].

If we incorrectly use ps[0], it would be like referencing a pivot index of -1, which doesn’t exist. Therefore, to correctly compare prefix sums with actual indices in nums, we should always reference ps[i + 1].

Related

LeetCode 1534: Count Good Triplets
·3 mins
LeetCode Blog Daily Easy Prefix Sum
LeetCode Problem Solving
LeetCode 303: Range Sum Query - Immutable
·1 min
LeetCode Blog Easy Prefix Sum
LeetCode Problem Solving
LeetCode 560: Subarray Sum Equals K
·2 mins
LeetCode Blog Daily Medium Prefix Sum
LeetCode Problem Solving
LeetCode 2843: Count Symmetric Integers
·1 min
LeetCode Blog Daily Easy Digit DP
LeetCode Problem Solving
LeetCode 617: Merge Two Binary Trees
·2 mins
LeetCode Blog Daily Easy Tree
LeetCode Problem Solving
LeetCode 1922: Count Good Numbers
·2 mins
LeetCode Blog Daily Medium
LeetCode Problem Solving