#2310
Medium Algorithms Sum of numbers with units digit k
Math Dynamic Programming Greedy Enumeration
28.1% acceptance
Feb 25, 2026
428
338
Given two integers num and k, consider a set of positive integers with the following properties:
The units digit of each integer is k. The sum of the integers is num.
Return the minimum possible size of such a set, or -1 if no such set exists.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn minimum_numbers(num: i32, k: i32) -> i32 {
if num == 0 {
return 0;
}
if k == 0 {
return if num % 10 == 0 { 1 } else { -1 };
}
for count in 1..=10i32 {
if (k * count) % 10 == num % 10 && k * count <= num {
return count;
}
}
-1
}
}