[LeetCode] Sum of Numbers With Units Digit K

2310. Sum of Numbers With Units Digit K

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.

Note:

  • The set can contain multiple instances of the same integer, and the sum of an empty set is considered 0.
  • The units digit of a number is the rightmost digit of the number.
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int minimumNumbers(int num, int k) {
if(num == 0) return 0;
for(int i = 1; i <= 10; i++) {
if(k * i <= num and k * i % 10 == num % 10)
return i;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/19/PS/LeetCode/sum-of-numbers-with-units-digit-k/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.