Data Structures & AlgorithmsVery hard

What is the length of the shortest contiguous subarray of 1 4 6 3 8 5 2 7 that contains at least 2 odd values AND at least 2 even values?

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

Correct answer: D. 4

Sliding window with two counters (odd and even): grow until both reach two, then shrink while they stay there. The minimum length is attained at a window whose ends are needed to reach the thresholds.

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

Exams that ask this topic