Find the Middle Index in Array

🏠 ⬅️ ➡️

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] <= 1000

Note: This question is the same as 724: https://leetcode.com/problems/find-pivot-index/


Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
!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
🌐 Data from online sources
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.

🌐 Data from online sources
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.