Leetcode 1480: Running Sum of 1d Array

Category : Easy

Problem

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.

https://leetcode.com/problems/running-sum-of-1d-array

Examples

Example 1:

Example 2:

Example 3:

Constraints

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

Solution Approach

In this question, you just need to find the running sum i.e the at any index of the array the sum should be equal to the total sum of all the previous indexes.

The solution to this problem is pretty straightforward. You just need to create a new array and its 0th index should have the value which is present at the 0th index in the original array. This is also supported by the fact that there will be at least one element in the array as stated in the problem. Now loop through each element in the array starting from 1 and keep adding the previous index’s value with the current value at the same index in the array given. It is a simple and easy example of Dynamic Programming.

Solution code

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding 

Leave a Comment

Your email address will not be published. Required fields are marked *