Power-Up Multipliers
In your card game, each player holds a row of power-up cards with multipliers: 3 triples your score, -1 flips it negative, 0 wipes it out. Your multiplier is all the cards multiplied together.
Before each turn the game discards one card, and under every card it shows the multiplier if that card were discarded. Dividing the full product by a card breaks as soon as a card is 0, so the scoring code only multiplies.
Your task
Write withoutEach(cards). Return a list out of the same length where out[i] is the product of every card except cards[i].
Do not use division. Aim for O(n) time.
withoutEach(cards: int[]) → int[]
Examples
cards = [2, 3, 4, 5]
[60, 40, 30, 24]
Discarding the 2 leaves 3 × 4 × 5 = 60, discarding the 3 leaves 2 × 4 × 5 = 40, discarding the 4 leaves 2 × 3 × 5 = 30, and discarding the 5 leaves 2 × 3 × 4 = 24.
cards = [3, 0, 2, -1]
[0, -6, 0, 0]
Every product that still contains the 0 is 0. Discarding the 0 gives 3 × 2 × (-1) = -6. Dividing the full product, which is 0, by the 0 card would fail here.
Constraints
- 2 ≤ cards.length ≤ 100,000
- -20 ≤ cards[i] ≤ 20
- The product of all the non-zero cards fits in a 32-bit signed integer.
Start with brute force
For each position, multiply all the other cards together.
That takes O(n²) time, about 10 billion multiplications for 100,000 cards, because neighbouring answers share most of their factors and the brute force recomputes them each time. Dividing the full product by each card would be O(n), but it fails when a card is 0 and is not allowed here.
Hints
0 of 4 shown- Hint 1
In
[2, 3, 4, 5], the answer under the 4 is 2 × 3 × 5. Which of those cards are left of the 4, and which right? - Hint 2
Every answer is (product of cards to the left) × (product of cards to the right). For the first card, the left side is empty and counts as 1.
- Hint 3
The left products for every position can be built in one left-to-right pass, each from the previous one. The right products work the same way from the other end.
- Hint 4
Fill
out[i]with the product of everything beforei, using a running product that starts at 1. Then walk from the right with a second running product, starting at 1: multiplyout[i]by it, then multiply it bycards[i].
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Removing card i splits the row into the cards before it and the cards after it. The answer is the product of the two parts, and each can be built one card at a time, like a running sum.
Trace [2, 3, 4, 5].
- The left pass writes the product of everything before each card, giving
out = [1, 2, 6, 24]. The first entry is 1 because nothing comes before it, and the later entries are 2, 2 × 3 and 2 × 3 × 4. - The right pass starts with
right = 1. At index 3, out[3] = 24 × 1 = 24, and then right becomes 5. - At index 2, out[2] = 6 × 5 = 30, and right becomes 20.
- At index 1, out[1] = 2 × 20 = 40, and right becomes 60.
- At index 0, out[0] = 1 × 60 = 60, so the result is
[60, 40, 30, 24].
In each pass, write the answer before multiplying in the current card, which keeps card i out of its own answer. Zeros need no special case, because a zero simply makes the running product 0 for every position after it.
function withoutEach(cards) {
const n = cards.length;
const out = new Array(n);
let left = 1; // product of cards before i
for (let i = 0; i < n; i++) {
out[i] = left;
left *= cards[i];
}
let right = 1; // product of cards after i
for (let i = n - 1; i >= 0; i--) {
out[i] *= right;
right *= cards[i];
}
return out;
}def without_each(cards):
n = len(cards)
out = [1] * n
left = 1 # product of cards before i
for i in range(n):
out[i] = left
left *= cards[i]
right = 1 # product of cards after i
for i in range(n - 1, -1, -1):
out[i] *= right
right *= cards[i]
return outRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.