Mid/SeniorGreedyMath

Minimum Addition to Make Integer Beautiful

You are given two positive integers n and target.

An integer is considered beautiful if the sum of its digits is less than or equal to target.

Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful.

Example 1
Inputn = 16, target = 6
Output4
Initially n is 16 and its digit sum is 7; after adding 4, n becomes 20 with digit sum 2, and no smaller non-negative addition works.
Example 2
Inputn = 467, target = 6
Output33
Initially n is 467 and its digit sum is 17; after adding 33, n becomes 500 with digit sum 5, and no smaller non-negative addition works.

Constraints

  • 1 <= n <= 10^12
  • 1 <= target <= 150
  • The input will be generated such that it is always possible to make n beautiful.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate