Data Structures & AlgorithmsHardGATE CS · TCS NQT

Moving one unit per step, what is the minimum total number of unit moves to make every element of [1, 2, 9, 10, 12] equal to a common value? (Use the median as target.)

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

Correct answer: B. 19

The sum of absolute deviations is minimised at the median; with median 9 the total is 8 + 7 + 0 + 1 + 3 = 19. The mean is a worse target for absolute deviation.

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

Exams that ask this topic