Sum of Numbers With Units Digit K
MEDIUMDescription
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.
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.
Constraints:
0 <= num <= 30000 <= k <= 9
Approaches
Checkout 3 different approaches to solve Sum of Numbers With Units Digit K. Click on different approaches to view the approach and algorithm in detail.
Brute-force Recursion
This approach attempts to solve the problem by exploring all possible combinations of numbers that end in k to see if they can sum up to num. A recursive function is defined to try subtracting every possible valid number (e.g., k, 10+k, 20+k, etc.) from the target num and finding the minimum count of numbers used in this process.
Algorithm
- Define a recursive function
solve(target)that returns the minimum number of integers ending inkthat sum totarget. - Base Case 1: If
target == 0, it means we have successfully formed the sum, so we need 0 more numbers. Return 0. - Base Case 2: If
target < 0, it's an invalid path. Return a value indicating impossibility, likeInteger.MAX_VALUE. - Recursive Step: Initialize a variable
minSizetoInteger.MAX_VALUE. - Iterate through all possible positive integers
cthat end with digitkand are less than or equal to the currenttarget(e.g.,k,10+k,20+k, ... or10,20, ... ifk=0). - For each
c, make a recursive callsolve(target - c). - If the recursive call does not return
Integer.MAX_VALUE, it means a solution was found for the subproblem. UpdateminSize = Math.min(minSize, 1 + solve(target - c)). - Return
minSize. - The initial call is
solve(num). If the result isInteger.MAX_VALUE, it means no solution exists, so return -1.
The core idea is to use a recursive function, let's call it solve(target), which calculates the minimum number of items to reach a target sum. The function works as follows:
- If the
targetis 0, it means we've found a valid combination, and the size of this sub-solution is 0. We return 0. - If the
targetbecomes negative, it means the last number we subtracted was too large, so this path is invalid. We return an indicator of failure, such as infinity. - For a positive
target, we iterate through all valid numberscthat we can use (positive integers ending ink). For eachc, we recursively callsolve(target - c). We are looking for the minimum result among1 + solve(target - c)over all possible choices ofc.
This method is a classic brute-force search. It explores the entire search space, but because it recalculates solutions for the same target values multiple times, its performance is very poor.
Complexity Analysis
Pros and Cons
- Conceptually simple and a direct translation of the problem statement.
- Extremely inefficient due to re-computation of the same subproblems.
- Will result in a 'Time Limit Exceeded' (TLE) error for the given constraints.
Code Solutions
Checking out 3 solutions in different languages for Sum of Numbers With Units Digit K. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Sum of Numbers With Units Digit K
Similar Questions
5 related questions you might find useful
Patterns:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.