Maximum Number of Integers to Choose From a Range II
A medium-tier problem at 35% community acceptance, tagged with Array, Binary Search, Greedy. Reported in interviews at PayPal and 0 others.
Maximum Number of Integers to Choose From a Range II shows up in PayPal assessments and trips up candidates who don't see the greedy pattern right away. With a 35% acceptance rate, this is a medium-difficulty problem that looks simpler than it is. You're asked to pick the maximum count of integers from a range while avoiding forbidden numbers. The naive approach of checking every integer fails on large ranges. The trick is greedy selection combined with binary search to skip over forbidden zones efficiently. If you blank on the pattern during the live assessment, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Maximum Number of Integers to Choose From a Range II"
Maximum Number of Integers to Choose From a Range II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe core insight is greedy: start at the lowest integer in your range and pick it if it's not forbidden, then jump to the next unpicked integer, repeating until you hit the range boundary. A linear scan works for small ranges, but large ranges (like 1 to 10^9) demand binary search on the forbidden list. Sort the forbidden numbers, then use binary search to find the next forbidden integer ahead of your current position. This cuts the algorithm from O(range_size) to O(forbidden_count * log(forbidden_count)). Most candidates either iterate naively and timeout, or forget to handle edge cases like forbidden integers equal to your current pick. StealthCoder is the hedge if you freeze on optimization. The problem tests your ability to fuse greedy choice-making with efficient search.
Pattern tags
You know the problem.
Make sure you actually pass it.
Maximum Number of Integers to Choose From a Range II recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Number of Integers to Choose From a Range II interview FAQ
Why does the simple loop fail on this problem?+
If the range spans 1 to 10^9, you can't iterate through every integer. You'd timeout. Instead, binary search lets you jump directly to the next forbidden number and skip the safe zone in between, cutting runtime from O(range) to O(forbidden_count * log(forbidden_count)).
Is this still asked at PayPal?+
Yes. It's in their assessment pool. The 35% acceptance rate means most candidates either miss the greedy insight or fail on edge cases like boundary conditions and large ranges. Know both approaches: linear for small ranges, binary search optimized for large ranges.
What's the greedy trick?+
Always pick the smallest available integer in your range, then move to the next one. This maximizes count because no other strategy captures more integers without violating the forbidden constraint. Pair this with binary search to avoid iterating through huge ranges.
How do I handle the binary search part?+
Sort the forbidden list. For each candidate integer, use binary search to check if it's forbidden. If forbidden, skip it and jump to the next non-forbidden integer. This replaces a naive linear scan and keeps you under time limits on large test cases.
What edge cases trip people up?+
Boundaries (is the range inclusive or exclusive?), empty forbidden lists, forbidden numbers outside the range, and integer overflow on large ranges. Also, ensure your binary search correctly identifies the next unpicked integer after skipping forbidden zones.
Want the actual problem statement? View "Maximum Number of Integers to Choose From a Range II" on LeetCode →