Left and Right Sum Differences

🏠 ⬅️ ➡️

Given a 0-indexed integer array nums, find a 0-indexed integer array answer where:

  • answer.length == nums.length.
  • answer[i] = |leftSum[i] - rightSum[i]|.

Where:

  • leftSum[i] is the sum of elements to the left of the index i in the array nums. If there is no such element, leftSum[i] = 0.
  • rightSum[i] is the sum of elements to the right of the index i in the array nums. If there is no such element, rightSum[i] = 0.

Return the array answer.

Example 1:

Input: nums = [10,4,8,3] Output: [15,1,11,22] Explanation: The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0]. The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].

Example 2:

Input: nums = [1] Output: [0] Explanation: The array leftSum is [0] and the array rightSum is [0]. The array answer is [|0 - 0|] = [0].

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 105

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solve the problem described in the task
    implicit none

    integer, parameter :: n = 4
    integer, dimension(n) :: nums = (/ 10, 4, 8, 3 /)
    integer, dimension(n) :: answer
    integer, dimension(n) :: leftSum, rightSum

    call solve(nums, answer, leftSum, rightSum)

    write (*, '(A)') 'Answer:'
    write (*, '(*(I0, 1X))') answer

contains

    subroutine solve(nums, answer, leftSum, rightSum)
        implicit none
        integer, intent(in) :: nums(:)
        integer, intent(out) :: answer(:), leftSum(:), rightSum(:)

        leftSum = 0
        do i = 1, size(nums)
            leftSum(i) = sum(nums(1:i))
        end do

        rightSum = 0
        do i = size(nums), 1, -1
            rightSum(i) = sum(nums(i+1:size(nums)))
        end do

        answer = abs(leftSum - rightSum)
    end subroutine solve
end program main
Compiled
Executed
Correct
module solution

implicit none

contains

function solve(nums) result(answer)

integer, intent(in) :: nums(:)
integer :: answer(size(nums))

! Left sum
integer :: leftSum(size(nums))
leftSum = 0
do i = 1, size(nums)
    leftSum(i) = sum(nums(1:i))
end do

! Right sum
integer :: rightSum(size(nums))
rightSum = 0
do i = size(nums), 1, -1
    rightSum(i) = sum(nums(i+1:size(nums)))
end do

! Answer
answer = abs(leftSum - rightSum)

end function solve

end module solution

program test

use solution
implicit none

integer, parameter :: nums1(4) = [10, 4, 8, 3]
integer, parameter :: answer1(4) = [15, 1, 11, 22]

integer, parameter :: nums2(1) = [1]
integer, parameter :: answer2(1) = [0]

integer, parameter :: nums3(2) = [1, 2]
integer, parameter :: answer3(2) = [1, 1]

integer, parameter :: nums4(3) = [1, 2, 3]
integer, parameter :: answer4(3) = [2, 1, 2]

integer, parameter :: nums5(4) = [1, 2, 3, 4]
integer, parameter :: answer5(4) = [2, 1, 2, 3]

integer, parameter :: nums6(5) = [1, 2, 3, 4, 5]
integer, parameter :: answer6(5) = [2, 1, 2, 3, 4]

integer, parameter :: nums7(6) = [1, 2, 3, 4, 5, 6]
integer, parameter :: answer7(6) = [2, 1, 2, 3, 4, 5]

integer, parameter :: nums8(7) = [1, 2, 3, 4, 5, 6, 7]
integer, parameter :: answer8(7) = [2, 1, 2, 3, 4, 5, 6]

integer, parameter :: nums9(8) = [1, 2, 3, 4, 5, 6, 7, 8]
integer, parameter :: answer9(8) = [2, 1, 2, 3, 4, 5, 6, 7]

integer, parameter :: nums10(9) = [1, 2, 3, 4, 5, 6, 7, 8, 9]
integer, parameter :: answer10(9) = [2, 1, 2, 3, 4, 5, 6, 7, 8]

integer, parameter :: nums11(10) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
integer, parameter :: answer11(10) = [2, 1, 2, 3, 4, 5, 6, 7, 8, 9]

integer, parameter :: nums12(11) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
integer, parameter :: answer12(11) = [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

integer, parameter :: nums13(12) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
integer, parameter :: answer13(12) = [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
🌐 Data from online sources
def get_answer(nums):
    n = len(nums)
    left_sum, right_sum, answer = [0] * n, [0] * n, [0] * n

    for i in range(1, n):
        left_sum[i] = left_sum[i - 1] + nums[i - 1]

    for i in range(n - 2, -1, -1):
        right_sum[i] = right_sum[i + 1] + nums[i + 1]

    for i in range(n):
        answer[i] = abs(left_sum[i] - right_sum[i])

    return answer
We create three arrays of the same length as `nums` called `leftSum`, `rightSum`, and `answer`.

We then iterate over the nums array starting from 1 and populate the leftSum array with the sums of elements to the left of the index i.

Similarly, we iterate over the nums array in reverse order (starting from the second last element) and populate the rightSum array with the sums of elements to the right of the index i.

Finally, we iterate over the leftSum and rightSum arrays and compute the absolute difference between the corresponding elements, storing the result in the answer array.

🌐 Data from online sources
#include <vector>
#include <cstdlib>
using namespace std;

vector<int> getAnswer(vector<int>& nums) {
    int n = nums.size();
    vector<int> leftSum(n, 0), rightSum(n, 0), answer(n, 0);

    for (int i = 1; i < n; i++) {
        leftSum[i] = leftSum[i - 1] + nums[i - 1];
    }

    for (int i = n - 2; i >= 0; i--) {
        rightSum[i] = rightSum[i + 1] + nums[i + 1];
    }

    for (int i = 0; i < n; i++) {
        answer[i] = abs(leftSum[i] - rightSum[i]);
    }

    return answer;
}
We create three arrays of the same length as `nums` called `leftSum`, `rightSum`, and `answer`.

We then iterate over the nums array starting from 1 and populate the leftSum array with the sums of elements to the left of the index i.

Similarly, we iterate over the nums array in reverse order (starting from the second last element) and populate the rightSum array with the sums of elements to the right of the index i.

Finally, we iterate over the leftSum and rightSum arrays and compute the absolute difference between the corresponding elements, storing the result in the answer array.