Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

How many contiguous subarrays of [3, 1, 2, 5, 4, 6] consist of distinct consecutive integers (in any order)?

  1. A. 7
  2. B. 21
  3. C. 6
  4. D. 12

Correct answer: D. 12

Distinct values form a consecutive run exactly when max − min = length − 1: the 6 singletons plus [3,1,2], [1,2], [5,4], [5,4,6], [3,1,2,5,4] and the whole array = 12. Requiring the run to be sorted gives 7, 21 counts all subarrays and 6 only singletons.

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

Exams that ask this topic