Data Structures & AlgorithmsHard

In [4, 7, 5, 9, 6, 3], how many contiguous subarrays have (max − min) ≤ 3?

  1. A. 17
  2. B. 7
  3. C. 11
  4. D. 5

Correct answer: C. 11

The spread of a subarray is not decided by its endpoints; an interior maximum or minimum can break the bound. Checking only |a[l] − a[r]| ignores the interior values, and using < instead of ≤ drops the boundary case.

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

Exams that ask this topic