Last Updated: 22 Dec, 2020

Product Array Puzzle

Easy
Asked in companies
BarclaysExpedia GroupAmazon

Problem statement

You are given an array of ‘N’ integers. You need to return another array ‘product’ such that ‘product[i]’ contains the product of all the arrays except the element at the ith position in the given array.

Note
As the product of elements can be very large you need to return the answer in mod (10^9+7).
Follow Up
Try to do this without using the division operator ‘/’, in constant space. The output array does not count as extra space for the purpose of space complexity analysis.
Input Format:
The first line of the input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains a single positive integer ‘N’ denoting the number of the elements present in the array.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
The only line of output of each test case should contain ‘N’ space-separated integers where ith integer denotes the product of all the elements in the array except itself.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10^2
2 <= N<= 10^4
1 <= A[i] <= 10^9 

Where ‘T’ is the number of test cases, ‘N’ is the size of the array,  ‘A[i]’ is the size of the array elements.

Time Limit: 1 sec

Approaches

01 Approach

  1. For each element, we need to find the product of all elements except the current element in the given array. We will create a ‘product’ array for storing the answer for each element.
  2. For each element, we run a loop and store the product of all elements in the array but as soon as we reach the index of the current element we should not include this in our product.
  3. After the loop is over we set the value of product[i] to the calculated product.
  4. Finally, after filling each position in ‘product’ we will return this array.

02 Approach

  1. We run a loop to store the product of the whole array in the ‘temp’ variable.
  2. Now for each element of the array, we will get the product of all elements except it by simply dividing the value of the ‘temp’ variable with the value of the current element i.e, product[i] = temp/arr[i].
  3. We calculate the product for each element and store the answer in the product array.

03 Approach

  1. We will prepare two extra arrays prefix[] and suffix[]. The prefix[i] will tell us the product of all the elements in the range [0, i] and similarly, suffix[i] will tell us the product of all the elements in the range [i, N-1].
  2. For calculating the value of prefix[] array we will run a loop and we will calculate prefix[i] as prefix[i-1] * a[i] because as per the assumption prefix[i-1] will contain the product to all elements from [0,i-1] so to calculate the product of range [0,i] we multiplied the product of range [0,i-1] with current element.
  3. Similarly we will calculate the suffix[i] as suffix[i+1] * a[i].
  4. After completing the above preprocessing steps we will directly use them to calculate our answer for each element.
  5. For each element, we want the product of the whole array except itself. So, if we carefully look at the range we want the product of range [0,i-1] and [i+1, N-1] in which ith element is excluded.
  6. We know that product of range [0,i-1] is prefix[i-1] and of range [i+1,N-1] is suffix[i] so we just multiply them to get our answer i.e, product[i] = prefix[i-1] * suffix[i+1].

04 Approach

  1. Instead of preparing two extra arrays prefix[] and suffix[]. We will use our ‘product’ array itself for serving both purposes one at a time.
  2. As we know from the previous approach that each element needs only one specific element of prefix[] and suffix[], so we will first store the value of prefix[i-1] in product[i] using the similar way explained in the previous approach to store the product of all elements in the range [0,i-1] in product[i].
  3. Now each ith element contains the product of range [0,i-1] but we also need the product of range [i+1, N-1] to get our final answer.
  4. So we keep a variable ‘temp’ which will store the product of the remaining suffix excluding the current element. we run loop a loop from end of the array and multiply prod[i] with temp i.e, product[i] = product[i] * temp and update the value of temp as temp = temp * a[i].
  5. Now look at the value of prod[i] in the first step we store the prefix the product at each position of prod[] and after that, we multiply each prod[i] with the product of suffix using the temp variable it means prod[i] now store the multiplication of product range [0,i-1] and [i+1, N-1] which is our desired answer for each ith element.