How to Find a Cumulative Sum Array in Java?


In Java, Array is an object. It is a non-primitive data type which stores values of similar data types.

As per the problem statement we have to find a cumulative sum array which means array elements will be updated with the sum of the current element and all previous elements.

To find the cumulative sum, the array must contain all numeric values.

In this article, you will see how to find the cumulative sum of an array by using Java programming language. Let’s explore.

To Show You Some Instances

Instance-1

Suppose the original array is {2, 9, 3, 5, 1, 6}

After updating the array with cumulative sum −

Cumulative sum is [2, 11, 14, 19, 20, 26]

Instance-2

Suppose the original array is {2, 9, 11, 5, 15, 6, 10}

After updating the array with cumulative sum −

Cumulative sum is [2, 11, 22, 27, 42, 48, 58]

Instance-3

Suppose the original array is {44, 5, 9, 15, 31, 22, 19, 48}

After updating the array with cumulative sum −

Cumulative sum is [44, 49, 58, 73, 104, 126, 145, 193]

Algorithm

  • Step 1 − Declare and initialize an integer array. Also declare and initialize an int variable say ‘sum’ as 0.

  • Step 2 − Traverse through the array.

  • Step 3 − Calculate the sum value as, sum = sum + array[i]

  • Step 4 − Replace the sum value with array[i]

  • Step 5 − Print the elements of the array.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array and for Loop

  • By Using Static Initialization of Array and while Loop

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array and for Loop

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm, find the cumulative sum of an array. Here, we have used a loop for traversing array.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args) {
      int sum = 0;
      
      //Declaring the array
      int numbers[] = { 44, 5 , 9, 15, 31, 22, 19, 48 };
      
      // traverse through the array
      for (int i = 0; i < numbers.length; i++) {
      
         // find sum
         sum += numbers[i];
         
         // replace
         numbers[i] = sum; 
      }
      
      //printing the result
      System.out.println("Cumulative sum is ");
      System.out.println(Arrays.toString(numbers));
   }
}

Output

Cumulative sum is 
[44, 49, 58, 73, 104, 126, 145, 193]

Approach-2: By Using Static Initialization of Array and while Loop

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm, find the cumulative sum of an array. Here, we have used a while loop for traversing array.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
      int sum = 0;
      
      //Declaring the array
      int numbers[] = { 44, 5 , 9, 15, 31, 22, 19, 48 };
      
      // traverse through the array
      int i =0;
      while(i<numbers.length){
      
         // find sum
         sum += numbers[i];
         
         // replace
         numbers[i] = sum; 
         i++;
      }
      
      //printing the result
      System.out.println("Cumulative sum is ");
      System.out.println(Arrays.toString(numbers));
   }
}

Output

Cumulative sum is 
[44, 49, 58, 73, 104, 126, 145, 193]

Approach-3: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm to find the cumulative sum of an array.

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
   
      //calling the user defined method
      cumulativeSum();   
   }
   
   //user defined method to find cumulative sum array
   public static void cumulativeSum(){
      int sum = 0;
      
      //Declaring the array
      int numbers[] = { 2, 9, 11, 5, 15, 6, 10 };
      
      // traverse through the array
      for (int i = 0; i < numbers.length; i++) {
      
         // find sum
         sum += numbers[i]; 
         
         // replace
         numbers[i] = sum; 
      }
      
      //printing the result
      System.out.println("Cumulative sum is ");
      System.out.println(Arrays.toString(numbers));
   }
}

Output

Cumulative sum is 
[2, 11, 22, 27, 42, 48, 58]

In this article, we explored how to find cumulative sum array by using Java programming language.

Updated on: 05-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements