Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the smallest amongst all the possible ones).
A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1].
If middleIndex == 0, the left side sum is considered to be 0. Similarly, if middleIndex == nums.length - 1, the right side sum is considered to be 0.
Return the leftmost middleIndex that satisfies the condition, or -1 if there is no such index.
Example 1:
Input: nums = [2,3,-1,8,4] Output: 3 Explanation: The sum of the numbers before index 3 is: 2 + 3 + -1 = 4 The sum of the numbers after index 3 is: 4 = 4
Example 2:
Input: nums = [1,-1,4] Output: 2 Explanation: The sum of the numbers before index 2 is: 1 + -1 = 0 The sum of the numbers after index 2 is: 0
Example 3:
Input: nums = [2,5] Output: -1 Explanation: There is no valid middleIndex.
Constraints:
1 <= nums.length <= 100-1000 <= nums[i] <= 1000Note: This question is the same as 724: https://leetcode.com/problems/find-pivot-index/
program main
! Solves the problem of finding the leftmost middleIndex in a 0-indexed integer array.
implicit none
integer :: i, j, k, n, middleIndex, leftSum, rightSum
integer, dimension(10) :: nums
write (*,*) "Example 1:"
nums = [2,3,-1,8,4]
middleIndex = findMiddleIndex(nums)
write (*,*) "middleIndex = ", middleIndex
write (*,*) "leftSum = ", leftSum
write (*,*) "rightSum = ", rightSum
write (*,*) "Example 2:"
nums = [1,-1,4]
middleIndex = findMiddleIndex(nums)
write (*,*) "middleIndex = ", middleIndex
write (*,*) "leftSum = ", leftSum
write (*,*) "rightSum = ", rightSum
write (*,*) "Example 3:"
nums = [2,5]
middleIndex = findMiddleIndex(nums)
write (*,*) "middleIndex = ", middleIndex
write (*,*) "leftSum = ", leftSum
write (*,*) "rightSum = ", rightSum
contains
function findMiddleIndex(nums) result(middleIndex)
! Finds the leftmost middleIndex in a 0-indexed integer array.
implicit none
integer, intent(in) :: nums(:)
integer :: middleIndex, leftSum, rightSum
do i=1,size(nums)-1
leftSum = 0
rightSum = 0
do j=0,i-1
leftSum = leftSum + nums(j)
end do
do k=i+1,size(nums)-1
rightSum = rightSum + nums(k)
end do
if (leftSum == rightSum) then
middleIndex = i
return
end if
end do
middleIndex = -1
end function findMiddleIndex
end program main
temp.f95:9:4:
9 | nums = [2,3,-1,8,4]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (10 and 5)
temp.f95:16:4:
16 | nums = [1,-1,4]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (10 and 3)
temp.f95:23:4:
23 | nums = [2,5]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (10 and 2)
temp.f95:41:41:
40 | do j=0,i-1
| 2
41 | leftSum = leftSum + nums(j)
| 1
Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
!include(sort.f90)
module FindMiddleIndex
! Declare the interface for the function
interface
function findMiddleIndex(nums) result(middleIndex)
integer, intent(in) :: nums(:)
integer :: middleIndex
end function findMiddleIndex
end interface
contains
! Implement the function
function findMiddleIndex(nums) result(middleIndex)
integer, intent(in) :: nums(:)
integer :: middleIndex, leftSum, rightSum, i
! Sort the array in ascending order
call sort(nums)
! Initialize the left sum and right sum to 0
leftSum = 0
rightSum = sum(nums)
! Iterate through the array
do i = 1, size(nums) - 1
! Update the left sum
leftSum = leftSum + nums(i)
! Update the right sum
rightSum = rightSum - nums(i)
! Check if the left sum is equal to the right sum
if (leftSum == rightSum) then
! If so, return the current index
middleIndex = i
exit
end if
end do
! If no middle index is found, return -1
if (middleIndex == 0) then
middleIndex = -1
end if
end function findMiddleIndex
end module FindMiddleIndex
! Test the function with some examples
program testFindMiddleIndex
use FindMiddleIndex
implicit none
integer :: nums(4), middleIndex
! Test case 1:
nums = [2, 3, -1, 8, 4]
middleIndex = findMiddleIndex(nums)
write (*,*) "Test case 1:", middleIndex
! Test case 2:
nums = [1, -1, 4]
middleIndex = findMiddleIndex(nums)
write (*,*) "Test case 2:", middleIndex
! Test case 3:
nums = [2, 5]
middleIndex = findMiddleIndex(nums)
write (*,*) "Test case 3:", middleIndex
end program testFindMiddleIndex
temp.f95:7:28:
7 | function findMiddleIndex(nums) result(middleIndex)
| 1
Error: MODULE attribute of ‘findmiddleindex’ conflicts with PROCEDURE attribute at (1)
temp.f95:8:38:
8 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in INTERFACE block at (1)
temp.f95:9:30:
9 | integer :: middleIndex
| 1
Error: Unexpected data declaration statement in INTERFACE block at (1)
temp.f95:10:7:
10 | end function findMiddleIndex
| 1
Error: Expecting END INTERFACE statement at (1)
temp.f95:16:24:
16 | function findMiddleIndex(nums) result(middleIndex)
| 1
Error: MODULE attribute of ‘findmiddleindex’ conflicts with PROCEDURE attribute at (1)
temp.f95:17:34:
17 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:18:48:
18 | integer :: middleIndex, leftSum, rightSum, i
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:21:19:
21 | call sort(nums)
| 1
Error: Unexpected CALL statement in CONTAINS section at (1)
temp.f95:24:15:
24 | leftSum = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:25:24:
25 | rightSum = sum(nums)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:28:28:
28 | do i = 1, size(nums) - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:30:35:
30 | leftSum = leftSum + nums(i)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:33:37:
33 | rightSum = rightSum - nums(i)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:36:37:
36 | if (leftSum == rightSum) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:38:27:
38 | middleIndex = i
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:39:16:
39 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:40:11:
40 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:41:7:
41 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:44:30:
44 | if (middleIndex == 0) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:45:24:
45 | middleIndex = -1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:46:7:
46 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:47:3:
47 | end function findMiddleIndex
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:53:9:
53 | use FindMiddleIndex
| 1
Fatal Error: Cannot open module file ‘findmiddleindex.mod’ for reading at (1): No such file or directory
compilation terminated.
def findMiddleIndex(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
Initially, calculate the total sum of the array elements. Iterate through the array and track the left_sum at every index. If the left_sum matches the updated sum of elements (total_sum - left_sum - current_number), then the current index is the middle index. If no such index is found, return -1.
int findMiddleIndex(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;
}
Initially, calculate the total sum of the array elements. Iterate through the array and track the left_sum at every index. If the left_sum matches the updated sum of elements (total_sum - left_sum - current_number), then the current index is the middle index. If no such index is found, return -1.