Ctrl K

Terms Sharing a Last Digit

Medium
You are given a non-negative integer total and a digit digit. Write total as a sum of positive integers that all end with the digit digit; the same number may be used more than once. Return the smallest possible number of terms, or -1 if total cannot be written this way. An empty set of terms sums to 0.
Example 1
total=44, digit=7 → 2

For example 17 + 27 = 44: two terms, both ending in 7. One term is not enough, since 44 ends in 4.

Example 2
total=15, digit=4 → -1

Any sum of numbers ending in 4 is even, and 15 is odd.

Example 3
total=0, digit=3 → 0

An empty set of terms already sums to 0.

Constraints
  • 0 ≤ total ≤ 10⁵
  • 0 ≤ digit ≤ 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsChunks With a Common Divisor MediumStrongest Product Team MediumTwo-Letter Tiles, No Triples MediumErase Digits Until It Divides by 25 MediumGrow and Copy Medium
loading Python…
Input
total=44, digit=7
Expected
2

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