Data Structures & AlgorithmsHard

A sorted array [1, 5, 10] is given. You may insert new positive integers into it. What is the minimum number of insertions so that every integer from 1 to 20 is a sum of some subset of the (extended) array? Each number may be used at most once.

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

Correct answer: A. 2

Greedy: while the reachable range [1..R] is short of the next value, insert R+1 because it doubles reach to 2R+1 at minimum cost. Brute force over insertions confirms the minimum; the count of initially missing values is not the answer.

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

Exams that ask this topic