Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

Two lists a = [4, 9, 1, 7] and b = [3, 8, 2, 10] are paired one-to-one (any bijection). What is the minimum possible value of the sum of |a_i − b_{σ(i)}| over all bijections σ?

  1. A. 6
  2. B. 7
  3. C. -2
  4. D. 4

Correct answer: D. 4

Sort both lists and pair in order: for absolute differences the sorted matching is optimal (an exchange argument removes any crossing). Pairing the lists in the given order can cross and cost more.

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

Exams that ask this topic