Data Structures & AlgorithmsHard

Each step adds 1 to one element of [2, 2, 2, 5, 5, 1]. What is the minimum total number of steps so that every element becomes distinct? (Values may only increase.)

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

Correct answer: D. 4

Sort the values and assign each one the smallest unused value not below it, which is optimal. Processing the array in its given order (without sorting) can cost more, and the duplicate count is a different quantity.

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

Exams that ask this topic