#2310
Sum of Numbers With Units Digit K
expert · 1185 · lc medium +32 · verified · 28.1% accepted · 428 likes · top 6%
Description
You are given two integers num and k. Find a set of positive integers where every element has k as its units digit and the elements sum to num.
Return the minimum number of elements in such a set, or -1 if it is impossible.
Note:
- The set may contain repeated values; the sum of an empty set is 0.
- The units digit is the rightmost digit.
Example 1:
Input: num = 58, k = 9
Output: 2
Explanation:
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
Another valid set is [19,39].
It can be shown that 2 is the minimum possible size of a valid set.
Example 2:
Input: num = 37, k = 2
Output: -1
Explanation: It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.
Example 3:
Input: num = 0, k = 7
Output: 0
Explanation: The sum of an empty set is considered 0.
Code
1
2
3