Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

From the numbers [7, 2, 9, 4, 1, 6, 3], choose the largest possible number of elements (each used at most once) whose total is at most 12. What is that maximum count?

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

Correct answer: B. 4

Take the smallest numbers first: for a count objective, the k smallest values minimise the total for that count, so the greedy sort is exact. Taking the largest numbers first uses up the budget too early.

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

Exams that ask this topic