Data Structures & AlgorithmsHard

In the 'maximum number of disjoint pairs with sum K' problem, why is a count-based greedy optimal?

  1. A. Pairs are always disjoint if the values are sorted.
  2. B. Each value can pair only with its complement K − v, so matching as many copies of each complementary pair as possible is optimal.
  3. C. The answer is always half the array length.
  4. D. Greedy works only when K is even.

Correct answer: B. Each value can pair only with its complement K − v, so matching as many copies of each complementary pair as possible is optimal.

Values only pair with complements, so the problem separates into independent groups per complementary class. Matching min(count[v], count[K−v]) pairs in each class is optimal.

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

Exams that ask this topic