InferenceAI

Enter at least two characters to search.

Start free

Product of All Others

mediumArrays & Hashing25 min25 XP

Return a list out where out[i] is the product of every element of nums except nums[i].

Requirements

  • Do not use division.
  • Run in O(n)O(n) time.

Constraints

  • 2≤n≤1052 \le n \le 10^5
  • Every prefix and suffix product fits in a 64-bit integer.

Examples

EXAMPLE WALKTHROUGHbasic

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3,4]]
TASKproduct_of_others()
EXPECTED OUTPUT[24,12,8,6]

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [1,2,3,4]

Output: [24,12,8,6]

Explanation: basic

Input: nums = [-1,1,0,-3,3]

Output: [0,0,9,0,0]

Explanation: contains a zero

Input: nums = [2,3]

Output: [3,2]

Explanation: two elements

Hint 1

out[i] = (product of everything to the left) × (product of everything to the right).

Hint 2

Fill left products in one pass, then multiply in right products in a reverse pass.

Topics

DSAArrays & HashingPython