Ctrl K

Numbers Within a Huge 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. Note that limit can be as large as a billion, so walking through the numbers one by one will not do.
Example 1
blocked=[2, 5], limit=10, budget=20 → 4

Take the smallest allowed numbers: 1, 3, 4 and 6, for a sum of 14. The next one, 7, would bring it to 21.

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

1 is forbidden, and every other number is above the budget.

Example 3
blocked=[3], limit=3, budget=1000000000000000 → 2
Constraints
  • 0 ≤ len(blocked) ≤ 10⁵
  • 1 ≤ blocked[i] ≤ limit ≤ 10⁹
  • 1 ≤ budget ≤ 10¹⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStick Triples That Form a Triangle MediumBest-Paid Gig for Each Worker MediumPrices That Fade With Each Sale MediumEven Out With Increments MediumRug Over Painted Stretches Medium
loading Python…
Input
blocked=[2, 5], limit=10, budget=20
Expected
4

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