Data Structures & AlgorithmsVery hard

In the greedy patching algorithm, the reachable range is [1..R] and the next sorted value x exceeds R+1. Why is R+1 the right patch?

  1. A. R+1 is the smallest value that is not reachable, and adding it extends the reachable range to 2R+1, the largest extension a single patch can give.
  2. B. Any value above R is always cheaper to add, so R+1 is never needed.
  3. C. A patch must always equal the sum of all current elements.
  4. D. R+1 is the only number that can be formed by an odd subset.

Correct answer: A. R+1 is the smallest value that is not reachable, and adding it extends the reachable range to 2R+1, the largest extension a single patch can give.

If R+1 is unreachable then it must be patched, and with [1..R] reachable adding R+1 makes every value up to 2R+1 reachable. Larger patches leave R+1 unreachable and so cost more later.

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

Exams that ask this topic