Left times right
transcript
Every account has a risk score. You want the product of all the others. The total divided by each score looks easy. A zero breaks it. Here is the safe way.
The total is zero. Dividing by zero is undefined. The middle answer should be eight. Division loses it. The middle answer should be eight. Division loses it, and no other trick recovers it.
Start the left product at one. At each position, store it before using the value. Then multiply the value in.
Walk back from the end with a right product. Multiply it into the stored left product. Then update it with the value.
Two passes over the list are enough. Each position is visited twice. No division is used, so no value can break the code. Only two running products are kept beyond the answer.
No division is used, so a zero is harmless. The answer at each position skips its own value. Two passes over the list are enough.
Never divide by a value that may be zero. Store the product on each side. Multiply the two at each position. Full breakdown in the lecture.