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.
- A. 2
- B. 1
- C. 3
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the smallest positive integer that CANNOT be written as the sum of some subset (each element used at most once) of the sorted…
- For the array [1, -2, 3, -1, 4, -6, 2], what is the maximum possible sum of a contiguous non-empty subarray after deleting AT MOST one of…
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1], you must delete exactly one element. What is the length of the longest run of consecutive 1s…
- How many index triples i < j < k in the sorted array [-2, 0, 1, 3, 5, 6] have A[i] + A[j] + A[k] strictly less than 4?
- For [2, 1, 3, 5, 4, 6, 8, 7, 9], how many indices i with 1 ≤ i ≤ n−2 satisfy: every element before i is strictly less than a[i], and every…
- How many split points p with 1 ≤ p ≤ n−1 divide [1, 2, 2, 3, 2, 4, 5] into a left part and a right part such that every element of the…