- 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

# Program to find running sum of 1d array in Python

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

## Example (Python)

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))

## Input

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

## Output

[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
- Program to find sum of absolute differences in a sorted array in Python
- Program to find maximum weighted sum for rotated 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 the 2 power sum of all subarray sums of a given array 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
- Flatten a 2d numpy array into 1d array in Python
- Program to find out the sum of evenly spaced elements in an array in Python
- Program to find sum of beauty of all substrings in Python

Advertisements