Product of Array Except Self

In this tutorial, I am going to discuss a very interesting problem Product of array except self or product of every integer except the integer at that index.

Given an array of N integers where N > 1. Write a code to print the result/output such that result[i] is equal to the product of all the elements of an array except self (result[i]).

For example –

Input: {4, 2, 1, 7}

Output: {14, 28, 56, 8}

NOTE: We have to solve this problem without division and in linear time O(n).

Product of Array Except Self

As per the problem statement, division operation is not allowed. Solving this problem using division operation is just a cakewalk. But the problem constraint is that we cannot use the division constraint. Before seeing the solution, Think for a moment about how you can solve this problem if division operation is not allowed.

I have shared the link of the video tutorial at the end of this post.

Product of Array Except Self with Division – Java Code

Suppose, if division constraint is not given then how we can solve this problem. In the next approach, we can solve this problem without using division operation.

i) First calculate the product of all the numbers of an array.

ii) Divide the product we obtain with the number present at each index.

We have discussed the approach to find product of array except self if division is allowed. Let’s solve this problem without using division operation.

Programming video tutorials

Find maximum sum of a subarray of size k

Product of Array Except Self

Product of Array Except Self – Java Code

To calculate the product at given i index. We need to find the product of all the numbers present to the left of i and product of all the numbers to the right of i. After that do the product of left and right numbers to get the answer.

I have also explained this approach using a video tutorial. The link is present at the end of this post.

Here are the following steps to implement this logic.

i) Declare two empty arrays left and right. The left[i] would contain the product of all the numbers to the left of i. The right[i] would contain the product of all the numbers to the right of i.

ii) Two fill these two arrays we need two for loops. For the left array, the value at left[0] would be 1. For rest of the elements we simply use this expression left[i] = left[i-1] * nums[i-1] where nums is the input array.

Now take right array and let’s do the same steps in a reverse order. The value at right[n-1] would be 1. For rest of the elements we use this expression right[i] = right[i+1] * nums[i+1].

iii) Once we filled the right and left array. Then the product at given index is the product of left[i] * right[i].

The time complexity of this approach is O(n). The space complexity is also O(n).

We can solve this problem in constant space by getting rid of the left and right arrays. In this example, let’s solve this problem without using extra space.

Product of array except self video tutorial

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.