Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Product Array Puzzle
EASY
15 mins
25 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Product Array Puzzle

Contributed by
Priyaraj
Easy
yellow-spark
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share
25 upvotes

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.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
5 
1 3 3 10 2
6 
2 4 6 3 1 1
Sample Output 1:
180 60 60 18 90
72 36 24 48 144 144
Explanation For Sample Input 1:
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  
Sample Input 2:
2
5 
1 10 1 2 2
6
2 12 1 1 20 1 
Sample Output 2:
40 4 40 20 20
240 40 480 480 24 480
Explanation For Sample Input 2:
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
Reset Code
Full screen
Auto
copy-code
Console