The Maximum Number of Integers to Choose From a Range II coding problem is the harder variant of Range I: same setup (banned list, range [1, n], maxSum) but the banned list and n can be much larger, requiring a more efficient solution than linear scan. You must use binary search over the sorted non-banned integers to find the maximum count efficiently.
PayPal includes this problem to test binary search proficiency in the context of filtered range queries. When n can be up to 10^9 but the banned list is small, a linear scan over [1, n] is infeasible. Candidates must binary search on the count k: "can I pick k unbanned integers from [1, n] with sum ≤ maxSum?" This tests both binary search design and prefix sum computation over ranges with holes.
Binary search on count k: Sort banned integers (filter those in [1, n]). Binary search on the answer k. For a given k, compute the minimum possible sum of k non-banned integers from [1, n]: pick the k smallest available. Use the sorted banned list to figure out which integers are available using prefix count of banned values up to each point. The minimum sum of the first k non-banned integers can be computed in O(log(n) + |banned|) using prefix sums over the banned list.
banned = [2, 4], n = 6, maxSum = 10.
For the Array Sorting Binary Search Greedy interview pattern, compare Range I (small n, use linear scan) vs Range II (large n, binary search on count) — this is a common interview follow-up. When n is huge but the excluded set is small, "binary search on answer + compute feasibility using excluded set" is the go-to pattern. Practice this template for maximum count under sum constraints.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximize Score of Numbers in Ranges | Medium | Solve | |
| Maximum Tastiness of Candy Basket | Medium | Solve | |
| Maximum Running Time of N Computers | Hard | Solve | |
| Maximum Number of Integers to Choose From a Range I | Medium | Solve | |
| Minimize the Maximum Difference of Pairs | Medium | Solve |