Product Of Array Except Self

Easy
0/40
Average time to solve is 26m
profile
Contributed by
344 upvotes
Asked in companies
FacebookDelhiveryIntuit

Problem statement

You have been given an integer array/list (ARR) of size N. You have to return an array/list PRODUCT such that PRODUCT[i] is equal to the product of all the elements of ARR except ARR[i]

 Note :
Each product can cross the integer limits, so we should take modulo of the operation. 

Take MOD = 10^9 + 7 to always stay in the limits.
Follow up :
Can you try solving the problem in O(1) space?
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case or query contains an integer 'N' representing the size of the array/list.

The second line contains 'N' single space-separated integers representing the elements in the array/list.
Output Format :
For each test case, print the elements of the 'PRODUCT' array separated by a single space. 

Output for every test case will be printed in a separate line.

Important Note :

You are required to return the product array and no need to print the result explicitly. It has already been taken care of.
Constraints :
1 <= T <= 100
0 <= N <= 10^5
0 <= ARR[i] <= 10^5

Time Limit: 1 sec
Sample Input 1 :
2
3
1 2 3
3
5 2 2
Sample Output 1 :
6 3 2
4 10 10
Explanation for Sample Output 1 :
 Test case 1 : Given array = {1, 2, 3] 
 Required array = [2 * 3, 1 * 3, 1 * 2] = [6, 3, 2]
 Test case 2 : Given array = {5, 2, 2] 
 Required array = [2 * 2, 5 * 2, 5 * 2] = [4, 10, 10]
Sample Input 2 :
2
1
100
2
1 2
Sample Output 2 :
1
2 1
Hint

For each index, think of computing the product of all the other elements.

Approaches (3)
Brute Force Approach

Traverse through every index of the array/list and compute the product of all the other elements in the array/list. This would require a new array/list of the same size as that of the input array/list in which we will store the product of the elements corresponding to every index.

1. Create a new array/list to store the product at every index

2. Start traversing on the input array/list till its length

3. For every ith iteration, find the product of all the elements to the left of i [0 - (i - 1)] and the product of all the elements to the right of i [(i + 1)  - (n -1)]

4. Take the product of left and right and assign the result to the i-th position in the new array keeping the result for every i

5. Repeat the above two steps till the end of the input array

6. Return the array in which you stored the products

Time Complexity

O(N^2) + O(N) , effectively, O(N^2), where N is the size of the input array/list.

 

For every iteration, we perform N-1 operations to calculate the left and right product making it a polynomial time. We do N operations at the end to copy the result to the input array.

Space Complexity

O(1).

Since we use constant space

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Product Of Array Except Self
Full screen
Console