Sum of Numbers with Units Digit K in Java

The task of finding numbers whose units digit is equal to k and their sum is equal to the given num is an interesting computational problem which can be solved using different approaches in Java.

Example 1

Input

Output:

 
2    

Explanation

We have to determine the minimum size of sets of integers with the unit's digit as 9 and the sum equals 58. Examples of valid sets are the sets [9, 49] and [19, 39], and others. The minimum size which can be achieved in a valid set is 2.

Example 2:

Input

Output:

 
-1    

Explanation

We need to find the minimum size of a set of integers with unit's digit 2 that sum up to 37. It is not possible to achieve a sum of 37 using only integers with a unit's digit of 2. Hence, the output is -1.

Approach 1: Using Dynamic Programming

Dynamic Programming (DP) is a powerful technique used to solve problems by breaking them down into smaller overlapping subproblems and storing the results of these subproblems to avoid redundant calculations.

Algorithm

Step 1: We create an array dp of size num + 1 to hold the minimum number of integers required to sum up to each value from 0 to num. We set dp[0] to 0 because no numbers are needed to sum up to 0.

Step 2: All other values in dp are initialized to Integer.MAX_VALUE to signify that those sums are initially unreachable.

Step 3: Filling the DP Array: We iterate through each possible sum j from 1 to num. For each sum j, we check if we can reach j by adding integers with unit's digit k.

Step 4: The integers with unit's digit k are of the form k, k+10, k+20, and so on. For each such integer i, if dp[j - i] is not Integer.MAX_VALUE, it means we can form the sum j by adding i to the sum j - i. We update dp[j] with the minimum number of integers required.

Step 5: After processing all sums, dp[num] will hold the minimum number of integers needed to sum up to num using integers with unit's digit k.

Step 6: If dp[num] is still Integer.MAX_VALUE, it indicates that it is not possible to form the sum num with integers having units digit k, so we return -1.

Filename: SumofNumbers.java

Output:

 
num = 58, k = 9: 2
num = 37, k = 2: -1
num = 0, k = 7: 0   

Time Complexity

The outer loop iterates through all possible sums from 1 to num, which gives us O(num) iterations. The inner loop iterates through all integers with unit's digit k, this is a constant number of iterations (up to 10 since the unit's digit ranges from 0 to 9). Therefore, the overall time complexity is O(num×10) = O(num).

Space Complexity

We use an array dp of size num + 1 to store the minimum number of integers required to achieve each sum from 0 to num. Therefore, the space complexity is O(num).

Approach 2: Optimized Approach

The optimized approach is used to systematically check all possible solutions. It involves iterating through all possible combinations and checking each one to see if it satisfies the problem conditions. The problem requires finding the minimum number of integers with a unit's digit k that sum up to num. The units digit of a number can range from 0 to 9.

Algorithm

Step 1: Initialize minimumNumbers method to check if num is 0. If true, return 0 immediately because an empty set sums up to 0.

Step 2: Iterative Checking: Iterate over possible counts i from 1 to 10 (since the units digit can only be from 0 to 9).

Step 3: For each i, calculate i * k and check if the units digit of i * k matches the units digit of num using i * k % 10 == num % 10.

Step 4: If a match is found, return i as the minimum number of integers needed to sum up to num.

Step 5: If no valid i is found after iterating through all possibilities (1 to 10), return -1 to indicate it is not possible to form num with integers having units digit k.

Filename: SumofNumbers1.java

Output:

 
num = 58, k = 9: 2
num = 37, k = 2: -1
num = 0, k = 7: 0   

Time Complexity

The minimumNumbers method iterates a fixed number of times (10 iterations), regardless of the size of num or k. It is because it loops from 1 to 10 to check all possible counts of integers with unit's digit k. These operations are constant time O(1). Therefore, the overall time complexity is constant O(1).

Space Complexity

The solution uses only a few constant space variables (num, k, i, count, and remaining), each of which requires a constant amount of space regardless of the input values. The space used by these variables does not change with the input size, hence the space complexity is O(1).