Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Cumulative Sum Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Cumulative Sum | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array, return its cumulative sum.

Naive Approach

Here, we can just iterate over the array and calculate the sum of all elements from the beginning till that index. The calculated sum for each index denotes the cumulative sum for that index.

Analysis

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

Implementation

C++
vector<int> getCumulativeSum(vector<int> &arr) {
   vector<int> cumulativeSum;
   for(int i = 0; i < arr.size(); i++) {
       int prefixSum = 0;
       for (int j = 0; j <= i; j++) {
           prefixSum += arr[j];
       }
       cumulativeSum.push_back(prefixSum);
   }
   return cumulativeSum;
}
Java
class Solution {
   int[] getCumulativeSum (int[] arr) {
       int prefixSum[] = new int[arr.length];
       for(int i = 0; i < arr.length; i++) {
           int prefix = 0;
           for(int j = 0; j <= i; j++) {
               prefix += arr[j];
           }
           prefixSum[i] = prefix;
       }
       return prefixSum;
   }
}
Python3
class Solution:
    def getCumulativeSum(self, arr: List[int]) -> List[int]:
        n = len(arr)
        cumulativeSum = [0] * n
        for i in range(n):
            prefixSum = 0
            for j in range(i + 1):
                prefixSum += arr[j]
            cumulativeSum[i] = prefixSum
        return cumulativeSum

Optimal Approach

Try to think about what is inefficient here.

We are calculating the prefix sum for the same set of indices multiple times. Is there a way to use the result of the previous prefix sum in the sum of the next prefix sum?

Observe that the result of the (i + 1)th prefix sum is equal to the result of the ith prefix sum + the current element in the array.

We can avoid recalculating the whole prefix sum at each index. We can just use the value of the prefix sum from the previous iteration and add the current array element to it.

Analysis

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

Implementation

C++
vector<int> getCumulativeSum(vector<int> &arr) {
   vector<int> cumulativeSum(n);
   cumulativeSum[0] = arr[0];
   for (int i = 1; i < arr.size(); i++) {
       cumulativeSum[i] = cumulativeSum[i - 1] + arr[i];
   }
   return cumulativeSum;
}
Java
class Solution {
   int[] getCumulativeSum (int[] arr) {
       int prefixSum[] = new int[arr.length];
       prefixSum[0] = arr[0];
       for(int i = 1; i < arr.length; i++) {
           prefixSum[i] = prefixSum[i - 1] + arr[i];
       }
       return prefixSum;
   }
}
Python3
class Solution:
    def getCumulativeSum(self, arr: List[int]) -> List[int]:
        n = len(arr)
        cumulativeSum = [arr[0]] * n
        for i in range(1, n):
            cumulativeSum[i] = cumulativeSum[i - 1] + arr[i]
        return cumulativeSum
Related Content
Even Number of Digits
Identical Twins
Largest Contiguous Sum
Matrix Rotation
Max Consecutive Ones
Next Greater Permutation
Pascal's Triangle
Positive Cumulative Sum
Primes upto N
Row Column Zero
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet