Ctrl K

Top Up to a Light Digit Sum

Medium
You are given positive integers n and limit. Call a number **light** if the sum of its digits does not exceed limit. Return the smallest non-negative integer x such that n + x is light. Such an x always exists.
Example 1
n=38, limit=5 → 2

38 has digit sum 11. Adding 2 gives 40 with digit sum 4 ≤ 5; adding less (39) only makes the sum bigger.

Example 2
n=1995, limit=3 → 5

2000 is the first number at least 1995 whose digit sum is at most 3, so the answer is 5.

Example 3
n=120, limit=3 → 0

The digit sum 1 + 2 + 0 = 3 already fits the limit, so nothing needs to be added.

Constraints
  • 1 ≤ n ≤ 10¹⁸
  • 1 ≤ limit ≤ 150
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmallest Number From Digit Factors MediumOne Digit Swap MediumLargest Number With Non-Decreasing Digits MediumFewest Guests by Lanyard Color MediumUp or Down for Every Post Medium
loading Python…
Input
n=38, limit=5
Expected
2

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