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

Positive Cumulative Sum Editorial

DSA Editorial, Solution and Code

Practice Problem Link:  Positive Cumulative Sum | Practice Problem

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

Problem Statement

The cumulative sum of an array at index i is defined as the sum of all elements of the array from index 0 to index i. The positive cumulative sum of an array is a list of only those cumulative sums which are positive. Given an array, return its positive 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. Store only those cumulative sums in a vector which have a positive value.

Analysis 

  • Time Complexity : O(n2) // due to 2 nested loops
  • Space Complexity : O(n)

Implementation 

C++
vector<int> getPositiveCumulativeSum(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);
   }
   vector<int> positiveCumulativeSum;
   for(auto x: cumulativeSum) {
       if(x > 0) {
           positiveCumulativeSum.push_back(x);
       }
   }
   return positiveCumulativeSum;
}
Java
class Solution {
   List<Integer> getPositiveCumulativeSum (int[] arr) {
       List<Integer> prefixSum = new ArrayList<Integer>();  
            for (int i = 0; i < arr.length; i++) {
               int prefix = 0;
               for (int j = 0; j <= i; j++) {
                   prefix += arr[j];
               }
               if(prefix > 0) {
                   prefixSum.add(prefix);
               }
            }
       return prefixSum;
   }
}

Optimal Approach

Whether the current prefix sum is positive or negative is easy to check, the main bottleneck is calculating the sums for each of the prefixes. Try to think about what we are doing inefficiently. We are calculating each prefix sum again and again. Is there a way to use the result of the previous prefix sum in the sum of the next prefix sum?

Here’s the key to solving this problem. The result of the (i + 1)th prefix is equal to the result of the ith prefix + current element in the array. So we don't need to recalculate the whole prefix sum, again and again, just use the value of the prefix sum from the previous iteration along with the current array element.

Now, only push those elements in a list, which are positive, and return it.

Analysis 

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

Implementation

C++
vector<int> getPositiveCumulativeSum(vector<int> &arr) {
   vector<int> prefix(arr.size());
   prefix[0] = arr[0];
   for (int i = 1; i < arr.size(); i++) {
       prefix[i] = prefix[i - 1] + arr[i];
   }
   vector<int> positivePrefix;
   for (int i = 0; i < arr.size(); i++) {
       if(prefix[i] > 0) {
           positivePrefix.push_back(prefix[i]);
       }
   }
   return positivePrefix;
}
Java
class Solution {
   List<Integer> getPositiveCumulativeSum (int[] arr) {
       List<Integer> positivePrefixSum = new ArrayList<Integer>();  
       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];
       }
       for (int i = 0; i < prefixSum.length; i++) {
           if(prefixSum[i] > 0) {
               positivePrefixSum.add(prefixSum[i]);
           }
       }
       return positivePrefixSum;
   }
}
Related Content
Cumulative Sum
Even Number of Digits
Identical Twins
Largest Contiguous Sum
Matrix Rotation
Max Consecutive Ones
Next Greater Permutation
Pascal's Triangle
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