1480. Running Sum of 1d Array LeetCode Solution

In this guide, you will get 1480. Running Sum of 1d Array LeetCode Solution with the best time and space complexity. The solution to Running Sum of d Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Running Sum of d Array solution in C++
  4. Running Sum of d Array solution in Java
  5. Running Sum of d Array solution in Python
  6. Additional Resources
1480. Running Sum of 1d Array LeetCode Solution image

Problem Statement of Running Sum of d 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1480. Running Sum of 1d Array LeetCode Solution in C++

class Solution {
 public:
  vector<int> runningSum(vector<int>& nums) {
    vector<int> ans;
    int sum = 0;
    for (const int num : nums)
      ans.push_back(sum += num);
    return ans;
  }
};
/* code provided by PROGIEZ */

1480. Running Sum of 1d Array LeetCode Solution in Java

class Solution {
  public int[] runningSum(int[] nums) {
    int[] ans = new int[nums.length];
    int sum = 0;
    for (int i = 0; i < nums.length; ++i)
      ans[i] = sum += nums[i];
    return ans;
  }
}
// code provided by PROGIEZ

1480. Running Sum of 1d Array LeetCode Solution in Python

class Solution:
  def runningSum(self, nums: list[int]) -> list[int]:
    return itertools.accumulate(nums)
# code by PROGIEZ

Additional Resources

See also  992. Subarrays with K Different Integers LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.