Trains the technique from
LeetCode 238Product of Array Except SelfThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A studio rack wires several amplifier stages in series. Stage i scales whatever signal reaches it by the integer factor gains[i]: a negative factor flips the signal's phase, and a factor of 0 means that stage is muted and kills the signal outright. The rack's overall factor is what you get by applying every stage in turn.
The engineer is auditioning patch cables, and for each stage wants to know the rack's overall factor when that single stage is jumpered out of the chain and all the other stages stay exactly as they are.
Given gains, return an array bypassed of the same length, where bypassed[i] is the factor the rack would have with stage i removed.
Your routine has to finish in linear time, and it may not use division anywhere: muted stages make dividing the rack's overall factor by a single stage's factor useless.
Example 1
Jumpering out the first stage leaves 1, 4 and 2, which combine to 8. Removing the second leaves 3 * 4 * 2 = 24, removing the third leaves 3 * 1 * 2 = 6, and removing the fourth leaves 3 * 1 * 4 = 12.
Example 2
Only removing the muted stage revives the rack, giving -2 * 5 * 3 = -30; every other chain still contains the muted stage and reads 0.
Example 3
Two stages are muted, so whichever single stage is jumpered out a muted one is still in the chain and the rack stays silent.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def bypass_gains(gains: list[int]) -> list[int]:public int[] bypassGains(int[] gains)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.