Data Structures & AlgorithmsHard
In 'product of array except self', why does a single zero need special handling?
- A. A zero is removed from the array before the products are computed.
- B. Zeros cause the prefix product to become negative.
- 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.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Why does counting subarrays with sum exactly k, when elements can be NEGATIVE, use a hash map of prefix sums and not a sliding window?
- When counting pairs with a given sum in a sorted array using two pointers, why is sorting essential?
- The 3-sum two-pointer method skips a value when it equals the previous value. Why?
- What is the lexicographically smallest array of length n with positive integers and total sum S (S ≥ n)?
- In the 'minimum swaps to sort a permutation' problem, why is the answer n minus the number of cycles?
- Which statement about a monotonic deque for sliding-window minimum is correct?