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/
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
temp.f95:48:28: 48 | if (.not. allocated(pivot_index)) then | 1 Error: ‘array’ argument of ‘allocated’ intrinsic at (1) must be ALLOCATABLE temp.f95:14:4: 14 | nums = [1, 2, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 3) temp.f95:17:4: 17 | nums = [2, 1, -1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 3)
```
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
```
temp.f95:1:2: 1 | ``` | 1 Error: Invalid character in name at (1) temp.f95:5:56: 5 | integer :: i, j, n, left_sum, right_sum, pivot_index | 1 Error: Symbol ‘pivot_index’ at (1) cannot have a type temp.f95:14:16: 14 | pivot_index = -1 | 1 Error: ‘pivot_index’ at (1) is not a variable temp.f95:25:24: 25 | pivot_index = i | 1 Error: ‘pivot_index’ at (1) is not a variable temp.f95:30:33: 30 | write (*, '(I0)') pivot_index | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:33:2: 33 | ``` | 1 Error: Invalid character in name at (1) temp.f95:10:8: 10 | do i = 1, n | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:18:12: 18 | do j = 1, i - 1 | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:16:16: 16 | left_sum = 0 | 1 Error: Symbol ‘left_sum’ at (1) has no IMPLICIT type temp.f95:8:13: 8 | read *, n | 1 Error: Symbol ‘n’ at (1) has no IMPLICIT type temp.f95:17:17: 17 | right_sum = 0 | 1 Error: Symbol ‘right_sum’ at (1) has no IMPLICIT type
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.
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.