Data Structures & AlgorithmsHard

Why does the cycle-based 'place each value at its index' method find missing numbers in linear time?

  1. A. The method sorts the array in quadratic time.
  2. B. Missing values are removed from the array first.
  3. C. Each element is compared with every other element.
  4. D. Each swap puts one value in its final position and never moves it again, so the total number of swaps is bounded by n.

Correct answer: D. Each swap puts one value in its final position and never moves it again, so the total number of swaps is bounded by n.

A correctly placed value is never disturbed again, so each swap fixes at least one position and the total work is linear. Positions left wrong then reveal the missing values.

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

Exams that ask this topic