Ctrl K

Numbers Within Budget

Medium
You are given a list of forbidden numbers blocked and two integers limit and budget. Choose a set of distinct integers such that: - every number lies between 1 and limit, inclusive; - none of them appears in blocked; - their sum does not exceed budget. Return the largest count of numbers you can choose under these rules.
Example 1
blocked=[2, 3], limit=8, budget=12 → 3

Take 1, 4 and 5 for a sum of 10. Adding the next allowed number, 6, would push the sum to 16.

Example 2
blocked=[9], limit=4, budget=100 → 4

9 is outside the range anyway, and 1 through 4 add up to 10, so all four fit.

Example 3
blocked=[1, 2], limit=5, budget=2 → 0

The smallest allowed number is 3, which already exceeds the budget.

Constraints
  • 0 ≤ len(blocked) ≤ 10⁵
  • 1 ≤ blocked[i] ≤ 10⁵
  • 1 ≤ limit ≤ 10⁵
  • 1 ≤ budget ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPairs With a Fixed Gap MediumStick Triples That Form a Triangle MediumJobs With a Cooldown MediumWords Hidden in a Text MediumBest-Paid Gig for Each Worker Medium
loading Python…
Input
blocked=[2, 3], limit=8, budget=12
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.