Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given an array **nums[]** of size **n**, construct a Product Array **P** (of same size n) such that** P[i]** is equal to the product of all the elements of **nums** except nums[i].

**Example 1:**

**Input:
**n = 5
nums[] = {10, 3, 5, 6, 2}
**Output:
**180 600 360 300 900**
Explanation: **
For i=0, P[i] = 3*5*6*2 = 180.
For i=1, P[i] = 10*5*6*2 = 600.
For i=2, P[i] = 10*3*6*2 = 360.
For i=3, P[i] = 10*3*5*2 = 300.
For i=4, P[i] = 10*3*5*6 = 900.

**Example 2:**

**Input:
**n = 2
nums[] = {12,0}
**Output:
**0 12

**Your Task:**

You do not have to read input. Your task is to complete the function **productExceptSelf() **that takes array nums[] and n as input parameters and returns a list of n integers denoting the product array P. If the array has only one element the returned list should should contains one value i.e {1}

**Note: **Try to solve this problem without using the division operation.

**Expected Time Complexity: **O(n)

**Expected Auxiliary Space: **O(n)

**Constraints:**

1 <= n <= 1000

0 <= nums_{i} <= 200

Array may contain duplicates.

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Product array puzzle

...

Here is some troubleshooting advice:

- Reset the IDE using the second button on the top right corner.
- Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
- You can access the hints to get an idea about what is expected of you as well as the final solution code.
- You can view the solutions submitted by other users from the submission tab.

Here is some troubleshooting advice for contest problems

- Make sure you are not using ad-blockers.
- Disable browser extensions.
- We recommend using latest version of your browser for best experience.
- Avoid using static/global variables in coding problems as your code is tested against multiple test cases and these tend to retain their previous values.
- Passing the Sample/Custom Test cases in coding problems does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.