Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 B

How many contiguous subarrays of [1, 3, 2, 4, 6, 5] have (maximum minus minimum) at most 2?

  1. A. 15
  2. B. 12
  3. C. 17
  4. D. 14

Correct answer: D. 14

Maintain the running max and min as the right end extends, so each subarray is checked once. Keeping only the max of the window and not the min misses valid cases.

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

Exams that ask this topic