Data Structures & AlgorithmsHard

What is the minimum number of elements you must DELETE from [5, 1, 9, 4, 8, 3, 7] (the remaining elements keep their order but need not be adjacent) so that the remaining values have max − min ≤ 3?

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

Correct answer: C. 4

Sort the values; the best kept set is a window of sorted values of width at most 3, and the answer is n minus its size. Restricting to contiguous kept elements loses scattered choices, while confusing the kept count with the deleted count gives the wrong side.

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

Exams that ask this topic