Data Structures & AlgorithmsHardCodeforces Div2 D · LeetCode-style

What is the length of the longest contiguous subarray of [4, 7, 5, 6, 8, 9, 7, 6, 3, 4] in which the largest value minus the smallest value is at most 2?

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

Correct answer: C. 3

Keep monotonic deques for the maximum and the minimum of the window; shrink from the left while max − min exceeds 2. Measuring the window by the first and last values, rather than the true max and min, misses interior extremes.

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

Exams that ask this topic