Data Structures & AlgorithmsVery hard

For 3 1 4 1 5 9 2 6 5 3, for each value that appears at least twice, add up the index gaps between consecutive occurrences of that value. What is the grand total over all values?

  1. A. 25
  2. B. 15
  3. C. 8
  4. D. 3

Correct answer: B. 15

Gaps between consecutive occurrences of one value telescope to the span from its first to its last occurrence, so summing per value is the same as summing last minus first. Counting duplicated values or the value range ignores the positions.

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

Exams that ask this topic