Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

For the array [7, 3, 9, 3, 5, 9, 1, 7], what is the maximum value of (j − i) over pairs of positions i < j with a[i] = a[j]?

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

Correct answer: D. 7

For each value, the farthest pair is its first and last occurrence, so record the first index of every value and measure each new occurrence against it. Comparing only adjacent equal values finds much smaller gaps.

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

Exams that ask this topic