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

Problem of the day

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.

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

Detailed explanation

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

```
2
5
1 3 3 10 2
6
2 4 6 3 1 1
```

```
180 60 60 18 90
72 36 24 48 144 144
```

```
Test case 1:
Product of elements except 1 = 3 * 3 * 10 * 2 = 180
Product of elements except 3 = 1 * 3 * 10 * 2 = 60
Product of elements except 3 = 1 * 3 * 10 * 2 = 60
Product of elements except 10 = 1 * 3 * 3 * 2 = 18
Product of elements except 2 = 1 * 3 * 3 * 10 = 90
Test case 2:
Product of elements except 2 = 4 * 6 * 3 * 1 * 1 = 72
Product of elements except 4 = 2 * 6 * 3 * 1 * 1 = 36
Product of elements except 6 = 2 * 4 * 3 * 1 * 1 = 24
Product of elements except 3 = 2 * 4 * 6 * 1 * 1 = 48
Product of elements except 1 = 2 * 4 * 6 * 1 * 1 = 144
Product of elements except 1 = 2 * 4 * 6 * 3 * 1 = 144
```

```
2
5
1 10 1 2 2
6
2 12 1 1 20 1
```

```
40 4 40 20 20
240 40 480 480 24 480
```

```
Test case 1:
Product of elements except 1 = 10 * 1 * 2 * 2 = 40
Product of elements except 10 = 1 * 1 * 2 * 2 = 4
Product of elements except 2 = 1 * 10 * 1 * 2 = 20
Product of elements except 2 = 1 * 10 * 1 * 2 = 20
Test case 2:
Product of elements except 2 = 12 * 1 * 1 * 20 * 1 = 240
Product of elements except 12 = 2 * 1 * 1 * 20 * 1 = 40
Product of elements except 1 = 2 * 12 * 1 * 20 * 1 = 480
Product of elements except 1 = 2 * 12 * 1 * 20 * 1 = 480
Product of elements except 20 = 2 * 12 * 1 * 1 * 1 = 24
Product of elements except 1 = 2 * 12 * 1 * 1* 20 = 480
```