Product Except Self

HardAcc. 84.4%
+45 XP 20

The Cumulative Multiplier

Imagine you need to calculate the influence of every element in a system except for itself. The most efficient way to do this without division is to pre-calculate the "influence from the left" and the "influence from the right."

The Assignment

Your mission is to calculate the product excluding the self. Your function receives nums.

  1. Create a prefix array where each index i stores the product of all elements to its left.
  2. Create a suffix array where each index i stores the product of all elements to its right.
  3. For each index, multiply the prefix and suffix values.
  4. Print the resulting products on separate lines.

01EXAMPLE 1

Input[1, 2, 3, 4]
Output24 12 8 6

Explanation: 1: 234=24. 2: 134=12, etc.

Constraints

  • Do not use the division operator (`/`).
  • O(n) time complexity.
ArraysPrefix Sum
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,2,3,4]

Expected Output

24
12
8
6

Click RUN to test your solution