Array Products I
Turn an array into one where each position holds the product of every other element: productOfOthers(nums). No division, O(n) time.
productOfOthers([3, 4, 5]) // [20, 15, 12] productOfOthers([1, 2, 3, 4]) // [24, 12, 8, 6]
Inputs are integers and may include zeros and negatives.
Hints
Hint 1
Fill the output with the product of everything to the left of each index, then sweep from the right and multiply in the product of everything to its right.
Hint 2
Division looks easier, but a single zero breaks it. The two sweeps need no special case.
Topics
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.