Data Structures & AlgorithmsHard

In 'product of array except self', why does a single zero need special handling?

  1. A. A zero is removed from the array before the products are computed.
  2. B. Zeros cause the prefix product to become negative.
  3. C. A zero makes every product except at its own index equal to zero, so the answer at that index is the product of the others only.
  4. D. Zeros are always ignored when multiplying.

Correct answer: C. A zero makes every product except at its own index equal to zero, so the answer at that index is the product of the others only.

The product excluding index i is zero whenever some other element is zero. So with exactly one zero, only that zero's position keeps a non-zero product; two zeros force every answer to zero.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic