Running Sum of 1d Array

🏠 ⬅️ ➡️

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]...nums[i]).

Return the running sum of nums.

Example 1:

Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Example 2:

Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

Example 3:

Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17]

Constraints:

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solves the problem "Running Sum of 1d Array" from LeetCode (https://leetcode.com/problems/running-sum-of-1d-array/)
    implicit none

    integer, parameter :: n = 5
    integer, dimension(n) :: nums
    integer, dimension(n) :: runningSum

    nums = [1, 2, 3, 4, 1]
    runningSum = runningSumOfArray(nums)
    write (*, '(A,*(I0,1X))') 'Running sum of ', nums, 'is', runningSum

contains

    function runningSumOfArray(nums) result(runningSum)
        implicit none
        integer, intent(in) :: nums(:)
        integer, dimension(size(nums)) :: runningSum

        integer :: i

        do i = 1, size(nums)
            runningSum(i) = sum(nums(0:i-1))
        end do
    end function runningSumOfArray
end program main
Compiled
Executed
Correct
```
program running_sum
    implicit none
    integer, parameter :: int64 = selected_int_kind(13)
    integer :: i, j, n
    integer(int64), allocatable :: nums(:), running_sum(:)

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

    ! compute running sum
    running_sum(1) = nums(1)
    do i = 2, n
        running_sum(i) = running_sum(i-1) + nums(i)
    end do

    ! print output
    do i = 1, n
        write(*,*) running_sum(i)
    end do

end program running_sum
```
🌐 Data from online sources
def runningSum(nums):
    for i in range(1, len(nums)):
        nums[i] += nums[i - 1]
    return nums

The solution for this problem is to iterate through the input array nums starting from the second element (index 1). In each iteration, we add the preceding element to the current element using the formula nums[i] += nums[i - 1]. After finishing the loop, the modified input array is the running sum of the original array.

The logic is exactly the same across all languages; we just have to appropriately manage syntax differences.

🌐 Data from online sources
#include <vector>
std::vector<int> runningSum(std::vector<int>& nums) {
    for(int i = 1; i < nums.size(); i++) {
        nums[i] += nums[i - 1];
    }
    return nums;
}

The solution for this problem is to iterate through the input array nums starting from the second element (index 1). In each iteration, we add the preceding element to the current element using the formula nums[i] += nums[i - 1]. After finishing the loop, the modified input array is the running sum of the original array.

The logic is exactly the same across all languages; we just have to appropriately manage syntax differences.