Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

Array a = [4, 7, 1, 8, 3, 6, 5, 2]. What is the largest sum of a contiguous subarray that contains exactly two odd numbers?

  1. A. 20
  2. B. 24
  3. C. 27
  4. D. 32

Correct answer: B. 24

Group prefixes by their odd-count so far: a subarray has exactly two odd numbers when its end and start prefixes differ by two in odd-count. For each end, the best start is the smallest prefix among the valid ones, which a running minimum over the counts provides.

More Coding Interview Problems questions

Exams that ask this topic