Find pivot index

November 5, 2022

array-and-hashmap

Problem URL: Find pivot index

We will iterate over the array, and calculate the sum of the left and right side of the current index. If the sum of the left side is equal to the sum of the right side, we will return the current index.

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

Time Complexity: O(n)
Space Complexity: O(1)