Data Structures & AlgorithmsModerate

What is the length of the longest contiguous subarray of 2 3 4 5 7 8 9 10 1 that contains at most 2 odd numbers?

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

Correct answer: B. 4

Sliding window with a count of odd elements: extend right, and shrink left whenever the odd count exceeds 2. Limiting to at most one or to exactly two odd numbers, or allowing three, changes the answer.

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

Exams that ask this topic