Ctrl K

Numbers With a Digit Sum in Bounds

Hard
You are given two positive integers low and high written as strings (they can be very long), and two integers min_total and max_total. Count the integers x with low ≤ x ≤ high whose digit sum is between min_total and max_total, inclusive. The count can be large, so return it modulo 10⁹ + 7.
Example 1
low="5", high="21", min_total=3, max_total=5 → 5

The digit sum is between 3 and 5 for 5, 12, 13, 14 and 21. For example, 11 and 20 have a digit sum of 2, which is too small. The answer is 5.

Example 2
low="1", high="100", min_total=10, max_total=10 → 9

Exactly 10 is the digit sum of 19, 28, 37, 46, 55, 64, 73, 82 and 91: nine numbers.

Example 3
low="999", high="1001", min_total=1, max_total=2 → 2

999 has a digit sum of 27, 1000 has 1 and 1001 has 2. The last two fit.

Constraints
  • low and high are decimal strings without leading zeros, at most 60 digits long
  • 1 ≤ low ≤ high
  • 1 ≤ min_total ≤ max_total ≤ 600
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNumbers From Working Keys HardFlip the Circuit Output HardGrading the Arithmetic Quiz HardSections of Two Seats HardTail to the Front Hard
loading Python…
Input
low="5", high="21", min_total=3, max_total=5
Expected
5

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