Open In App

Find Cumulative Sum Of a List, Vector or Array

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of numbers, the task is to find and return a list with the cumulative sum of given list.

Examples : 

Input: list = [10, 20, 30, 40, 50]
Output : [10, 30, 60, 100, 150]
Explanation:
for cumulative sum, every index of the output list will be list[i] = list[i] + originallist[i], therefore the resultant list will be [10, 10+20, 10+20+30, 10+20+30+40, 10+20+30+40+50] = [10, 30, 60, 100, 150]

Input: list = [4, 10, 15, 18, 20]
Output : [4, 14, 29, 47, 67]

Approach:

  • Create another empty list of the same size
  • Copy the first element of the original list into the new list as the first element.
  • Iterate the original list add the current element to the previous element of the new list and store it as the current element of the new list, i.e., newList[i] = oldList[i]+newList[i-1]
  • Return the newList

Below is the implementation of the above approach:

C++




// C++ code to find
// cumulative sum of list
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the cumulative sum list
vector<int> cumulativeList(vector<int> oldList, int n)
{
    vector<int> newList(n, 0);
 
    // Adding 1st element in the result list
    newList[0] = oldList[0];
 
    // Adding current element in the cumulative list
    for (int i = 1; i < n; i++)
        newList[i] = oldList[i] + newList[i - 1];
 
    return newList;
}
 
// Driver code
int main()
{
 
    vector<int> list = { 10, 20, 30, 40, 50 };
    int n = 5;
 
    for (int i = 0; i < n; i++)
        cout << list[i] << " ";
    cout << "\n";
 
    vector<int> cumuList = cumulativeList(list, n);
 
    for (int x : cumuList)
        cout << x << " ";
 
    return 0;
}


Java




// Java Implementation
 
import java.util.ArrayList;
import java.util.List;
 
public class CumulativeSum {
    public static List<Integer> cumulativeList(List<Integer> oldList) {
        List<Integer> newList = new ArrayList<>();
        int sum = 0;
         
        for (int i = 0; i < oldList.size(); i++) {
            sum += oldList.get(i);
            newList.add(sum);
        }
         
        return newList;
    }
     
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
         
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
         
        List<Integer> cumuList = cumulativeList(list);
         
        for (int x : cumuList) {
            System.out.print(x + " ");
        }
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Python3




# Function to return the cumulative sum list
def cumulative_list(old_list):
    new_list = [0] * len(old_list)
 
    # Adding the first element in the result list
    new_list[0] = old_list[0]
 
    # Adding the current element in the cumulative list
    for i in range(1, len(old_list)):
        new_list[i] = old_list[i] + new_list[i - 1]
 
    return new_list
 
 
# Driver code
if __name__ == "__main__":
    old_list = [10, 20, 30, 40, 50]
 
    print("Original List:")
    for item in old_list:
        print(item, end=" ")
    print()
 
    cumulative_list_result = cumulative_list(old_list)
 
    print("Cumulative Sum List:")
    for item in cumulative_list_result:
        print(item, end=" ")


C#




using System;
using System.Collections.Generic;
 
class CumulativeSumList
{
    // Function to return the cumulative sum list
    static List<int> CumulativeList(List<int> oldList, int n)
    {
        List<int> newList = new List<int>(n);
 
        // Adding 1st element in the result list
        newList.Add(oldList[0]);
 
        // Adding current element in the cumulative list
        for (int i = 1; i < n; i++)
        {
            newList.Add(oldList[i] + newList[i - 1]);
        }
 
        return newList;
    }
 
    // Driver code
    static void Main()
    {
        List<int> list = new List<int> { 10, 20, 30, 40, 50 };
        int n = 5;
 
        foreach (int num in list)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
 
        List<int> cumuList = CumulativeList(list, n);
 
        foreach (int x in cumuList)
        {
            Console.Write(x + " ");
        }
    }
}
 
// This code is contributed by shivamgupta310570


Javascript




// Function to return cumulative sum array 
 
function cumulativeArray(oldArray){
    newArray = Array(oldArray.length).fill(0);
     
    // Adding first element in the result array
    newArray[0] = oldArray[0];
 
    // Adding the current element in the cumulative array
    for (let i = 1; i < newArray.length; i++) {
        newArray[i] = oldArray[i] + newArray[i - 1];
    }
 
    return newArray;
}
 
// Driver Code
 
const oldArray = [10, 20, 30, 40, 50];
 
console.log("Original Array:");
console.log(oldArray.join(" "));
 
const cumulativeSumArray = cumulativeArray(oldArray);
 
console.log("Cumulative Sum Array:");
console.log(cumulativeSumArray.join(" "));


Output

10 20 30 40 50 
10 30 60 100 150 




Complexity Analysis:

  • Time complexity: O(N), to iterate previous list
  • Auxillary Space: O(N), for new list


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads