Skip to main content

Command Palette

Search for a command to run...

product Arrays

Updated
3 min readView as Markdown

Problem Statement: Given an array nums of size n, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

  • Input: nums = [1, 2, 3, 4]

  • Output: [24, 12, 8, 6]

Explanation:

  • The output array should be such that:

    • output[0] = Product of all elements except nums[0] = 2 3 4 = 24

    • output[1] = Product of all elements except nums[1] = 1 3 4 = 12

    • output[2] = Product of all elements except nums[2] = 1 2 4 = 8

    • output[3] = Product of all elements except nums[3] = 1 2 3 = 6

The problem can be solved using a division method efficiently.

Solution Explanation

Approach Using Division:

  1. Calculate the Total Product:
    First, calculate the product of all elements in the array. This product will help us determine the value of each element in the output array by dividing it by the corresponding element in the input array.

    For example, if nums = [1, 2, 3, 4], the total product is: 1*2*3*4= 24

    Create the Output Array:
    For each element in the array, divide the totalProduct by the element at that index to get the product of all other elements. This value will be stored in the output array.

    For example:

    • output[0] = totalProduct / nums[0] = 24 / 1 = 24

    • output[1] = totalProduct / nums[1] = 24 / 2 = 12

    • output[2] = totalProduct / nums[2] = 24 / 3 = 8

    • output[3] = totalProduct / nums[3] = 24 / 4 = 6

Java Implementation Using long

This implementation ensures that even with large numbers, the product calculations will not overflow by using the long data type.

public class ProductArray {
    public static long[] productExceptSelf(long[] nums) {
        int n = nums.length;
        long[] output = new long[n];

        // Step 1: Calculate the total product of all elements using long to handle large numbers
        long totalProduct = 1;
        for (int i = 0; i < n; i++) {
            totalProduct *= nums[i];
        }

        // Step 2: Populate the output array
        for (int i = 0; i < n; i++) {
            output[i] = totalProduct / nums[i];
        }

        return output;
    }

    public static void main(String[] args) {
        long[] nums = {1, 2, 3, 4};  // Example with small numbers, can be replaced with larger ones
        long[] result = productExceptSelf(nums);

        // Print the result
        for (long value : result) {
            System.out.print(value + " ");
        }
    }
}

Output:

For the input nums = {1, 2, 3, 4}, the program will output:

Copy code24 12 8 6

Considerations:

  1. Division Method:

    • This method is simple and straightforward but has limitations if the array contains zeros because division by zero is undefined.

    • This solution assumes that no zeros are present in the input array.

  2. Efficiency:

    • The time complexity is O(n), as we only need to iterate through the array twice (once for calculating the product and once for filling the output array).

    • The space complexity is O(n), where n is the length of the array, for storing the output.

More from this blog

Amit singh's blog

235 posts