Data Structures & AlgorithmsHard

Values in 4 3 2 7 8 2 3 1 lie in the range 1 to 8 (length 8). Using cyclic sort (place each value v at index v−1 by swaps), which values of 1..8 are MISSING from the array? Give them in increasing order.

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

Correct answer: C. 5 6

After cyclic placement each position k holds value k+1 when it is present; the positions still wrong identify the missing values. Taking the complement of the array as a set gives the same answer, so an incorrect comparison (like sorting the input) is what causes mistakes here.

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

Exams that ask this topic