Find Pivot Index

🏠 ⬅️ ➡️

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 index's right.

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

Constraints:

  • 1 <= nums.length <= 104
  • -1000 <= nums[i] <= 1000

Note: This question is the same as 1991: https://leetcode.com/problems/find-the-middle-index-in-array/


Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solves the problem of finding the pivot index of an array of integers
    implicit none

    integer, parameter :: n = 6
    integer, dimension(n) :: nums
    integer :: i, j, k, l, m, n_left, n_right, pivot_index
    logical :: found

    ! Examples
    nums = [1, 7, 3, 6, 5, 6]
    write (*, '(A, I0)') 'Example 1: ', find_pivot_index(nums)

    nums = [1, 2, 3]
    write (*, '(A, I0)') 'Example 2: ', find_pivot_index(nums)

    nums = [2, 1, -1]
    write (*, '(A, I0)') 'Example 3: ', find_pivot_index(nums)

contains

    function find_pivot_index(nums) result(pivot_index)
        ! Calculates the pivot index of an array of integers
        implicit none

        integer, intent(in) :: nums(:)
        integer :: pivot_index
        integer :: i, n_left, n_right

        n_left = 0
        n_right = sum(nums)

        do i = 1, size(nums)
            if (n_left == n_right) then
                pivot_index = i - 1
                exit
            end if

            if (i > 1) then
                n_left = n_left + nums(i - 1)
            end if

            if (i < size(nums)) then
                n_right = n_right - nums(i)
            end if
        end do

        if (.not. allocated(pivot_index)) then
            pivot_index = -1
        end if

    end function find_pivot_index

end program main
Compiled
Executed
Correct
```
program pivot_index
    implicit none
    integer, parameter :: int64 = selected_int_kind(13)
    integer :: i, j, n, left_sum, right_sum, pivot_index
    integer, dimension(:), allocatable :: nums

    read *, n
    allocate(nums(n))
    do i = 1, n
        read *, nums(i)
    end do

    pivot_index = -1
    do i = 1, n
        left_sum = 0
        right_sum = 0
        do j = 1, i - 1
            left_sum = left_sum + nums(j)
        end do
        do j = i + 1, n
            right_sum = right_sum + nums(j)
        end do
        if (left_sum == right_sum) then
            pivot_index = i
            exit
        end if
    end do

    write (*, '(I0)') pivot_index

end program pivot_index
```
🌐 Data from online sources
def pivotIndex(nums):
    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
1. First, find the total sum of the array.
  1. Iterate through the array, maintaining a running sum of the elements to the left of the current index.
  2. For each index, check if the left sum is equal to the total sum minus the left sum and the value at the current index.
  3. If such equality is found, return the index as the pivot index. If not found, return -1.
🌐 Data from online sources
int pivotIndex(vector<int>& nums) {
    int total_sum = 0, left_sum = 0;
    for (int num : nums)
        total_sum += num;
    for (int i = 0; i < nums.size(); ++i) {
        if (left_sum == total_sum - left_sum - nums[i])
            return i;
        left_sum += nums[i];
    }
    return -1;
}
1. First, find the total sum of the array.
  1. Iterate through the array, maintaining a running sum of the elements to the left of the current index.
  2. For each index, check if the left sum is equal to the total sum minus the left sum and the value at the current index.
  3. If such equality is found, return the index as the pivot index. If not found, return -1.