Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 22 Dec, 2020

Easy

```
As the product of elements can be very large you need to return the answer in mod (10^9+7).
```

```
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.
```

```
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.
```

```
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.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
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
```

- 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.
- 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.
- After the loop is over we set the value of product[i] to the calculated product.
- Finally, after filling each position in ‘product’ we will return this array.

- We run a loop to store the product of the whole array in the ‘temp’ variable.
- 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].
- We calculate the product for each element and store the answer in the product array.

- 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]. - 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. - Similarly we will calculate the
*suffix[i]*as*suffix[i+1] * a[i]*. - After completing the above preprocessing steps we will directly use them to calculate our answer for each element.
- 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.
- 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].*

- Instead of preparing two extra arrays
*prefix[]*and*suffix[]*. We will use our ‘product’ array itself for serving both purposes one at a time. - 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].
- 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.
- 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].* - 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.

Similar problems

Longest Subarray With Zero Sum

Moderate

Posted: 3 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Ninja And The Strictly Increasing Array

Moderate

Posted: 27 Nov, 2022

Negative To The End

Easy

Posted: 16 Dec, 2022

Find Duplicate in Array

Easy

Posted: 5 Jun, 2023