76. Everyone But Me

RPby Rohit Parihar1 submission

A relay team has one score multiplier per runner, stored in the array nums. For a strategy report, the coach needs, for each runner, the combined multiplier of all the other runners, that is, the product of every multiplier except the runner's own.

Return an array answer of the same length as nums where answer[i] equals the product of all elements of nums except nums[i]. You must not use division, since some multipliers are zero. It is guaranteed that every value of answer fits in a 32-bit signed integer, although partial products you compute along the way might not.

Example 1

Input:
nums = [2,3,-1,4]
Output:
[-12,-8,24,-6]
Explanation:

Leaving out each position in turn gives 3*(-1)*4, 2*(-1)*4, 2*3*4 and 2*3*(-1).

Example 2

Input:
nums = [0,5,2]
Output:
[10,0,0]
Explanation:

Only the position holding the zero has a nonzero product of the others, namely 5*2.

Constraints

2 ≤ nums.length ≤ 106

-105 ≤ nums[i] ≤ 105

Every element of the answer fits in a 32-bit signed integer.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…