Data Structures & AlgorithmsHard

How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] have equal first and last elements and length at least 2?

  1. A. 13
  2. B. 5
  3. C. 6
  4. D. 3

Correct answer: C. 6

Each equal-value pair at positions i < j gives one subarray with equal ends, so this is a pair-count per value. Counting singletons adds all n trivially equal ones, requiring length ≥ 3 drops the length-2 cases, and the distinct-value count is unrelated.

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

Exams that ask this topic