Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

What is the length of the longest contiguous subarray of [2, 4, 1, 3, 6, 7, 8, 5, 9] containing at most two odd numbers?

  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 7

Correct answer: C. 5

Sliding window with a count of odd elements: shrink whenever the count exceeds two. The best window spans from 6 to 9 except for the extra odd values.

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

Exams that ask this topic