Data Structures & AlgorithmsHard

What is the length of the shortest contiguous subarray of 3 8 1 5 8 2 3 7 whose two END elements are equal?

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

Correct answer: A. 4

For each value take the closest pair of equal positions; the length counts both ends, so add one to the index difference. Measuring the gap without adding one, or taking the widest pair, gives wrong values.

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

Exams that ask this topic