- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array nums. The running sum of an array as rs[i] is sum of all elements from nums[0] to nums[i]. Finally return the entire running sum of nums.

So, if the input is like nums = [8,3,6,2,1,4,5], then the output will be [8, 11, 17, 19, 20, 24, 29], because

rs[0] = nums[0] rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so on

To solve this, we will follow these steps −

n:= size of nums

rs:= [nums[0]]

for i in range 1 to n - 1, do

nums[i] := nums[i] + nums[i-1]

insert nums[i] at the end of rs

return rs

Let us see the following implementation to get better understanding −

def solve(prices): n=len(nums) rs=[nums[0]] for i in range(1,n): nums[i]+=nums[i-1] rs.append(nums[i]) return rs nums = [8,3,6,2,1,4,5] print(solve(nums))

[8,3,6,2,1,4,5]

[8, 11, 17, 19, 20, 24, 29]

- Related Questions & Answers
- Python Program to find the sum of array
- Find the sum of array in Python Program
- Flatten a 2d numpy array into 1d array in Python
- Python program to find sum of elements in list
- Program to find sum of unique elements in Python
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find sum of contiguous sublist with maximum sum in Python
- Java program to find the sum of elements of an array
- Python program to find Cumulative sum of a list
- Python program to find the sum of sine series
- Program to find diagonal sum of a matrix in Python
- Program to find sum of rectangle whose sum at most k in Python
- Find sum of elements in list in Python program
- Program to find sum of elements in a given array in C++
- Program to find largest sum of 3 non-overlapping sublists of same sum in Python

Advertisements