Data Structures & AlgorithmsHardCodeforces Div2 B · GATE CS

Each move changes an element of [1, 2, 9, 10, 4, 4, 3] by exactly 1 (up or down). What is the minimum number of moves needed to make all elements equal to the same value?

  1. A. 20
  2. B. 17
  3. C. -5
  4. D. 18

Correct answer: B. 17

The sum of |aᵢ − x| is minimised at a median of the values, not the mean; for an odd count the median is the middle element after sorting. The mean minimises the sum of squared deviations instead, and gives a larger cost here.

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

Exams that ask this topic