Calculating the cumulative product of elements in a Java array

Calculating the cumulative product of elements in an array is a common task in programming. In this blog post, we will explore a simple and efficient way to calculate the cumulative product of elements in a Java array.

The Problem

Given an array of integers, we want to calculate a new array where each element is the product of all the elements before it (including itself).

For example, given the input array: [1, 2, 3, 4, 5], the expected output array would be: [1, 2, 6, 24, 120].

The Solution

To solve this problem, we can use a simple algorithm that keeps track of the cumulative product as we iterate through the input array.

Here is the step-by-step process to calculate the cumulative product:

  1. Create a new array of the same size as the input array to store the results.
  2. Initialize the first element of the new array with 1, as there are no elements before it.
  3. Iterate through the input array from the second element to the last element.
  4. For each element, multiply it by the previous element in the input array and store the result in the corresponding position of the new array.
  5. Return the new array with the cumulative product.
public class CumulativeProductCalculator {
    public static int[] calculateCumulativeProduct(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        result[0] = 1;
        
        for (int i = 1; i < n; i++) {
            result[i] = result[i - 1] * nums[i - 1];
        }
        
        return result;
    }
}

Testing the Solution

To test the solution, we can call the calculateCumulativeProduct method on different input arrays and verify that the output is correct.

public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        int[] result = CumulativeProductCalculator.calculateCumulativeProduct(nums);
        
        System.out.println(Arrays.toString(result)); // Output: [1, 2, 6, 24, 120]
    }
}

Conclusion

Calculating the cumulative product of elements in a Java array can be easily achieved using a simple algorithm that iteratively computes the products. By following the steps outlined in this blog post, you can efficiently calculate the cumulative product of any input array.