Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

How many contiguous subarrays of [4, 2, 3, 1, 6, 5, 7, 9] satisfy max − min = (length of the subarray) − 1? Count every such subarray, including those with repeated values.

  1. A. 16
  2. B. 18
  3. C. 15
  4. D. 8

Correct answer: A. 16

Repeated values can also satisfy max − min = length − 1 (for example [1, 3, 3]), so the test must be done on the max and min directly rather than by assuming distinct values. With sliding-window max and min deques, each window is tested in amortised O(1). Counting the increasing runs answers a different question.

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

Exams that ask this topic