Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many contiguous subarrays of [1, 2, 2, 3, 1, 1] contain no two adjacent equal elements?

  1. A. 11
  2. B. 10
  3. C. 8
  4. D. 13

Correct answer: B. 10

A subarray is valid exactly when it lies inside a maximal stretch with no equal neighbours, so count per stretch with L(L+1)/2.

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

Exams that ask this topic