Given a positive integer N and a digit K, the task is to find the minimum count of numbers ending with digit K such that the sum of those numbers is N. If no such number exists whose sum is K, then print “-1”.
Examples:
Input: N = 42, K = 3
Output: 4
Explanation:
The given number N(= 43) can be expressed as 3 + 3 + 13 + 23, such all the numbers ends with digit K(= 3). Therefore, the count split numbers 4.Input: N = 17, K = 3
Output: -1
Approach: The given problem can be solved by an observation that if a number can be expressed as the sum of numbers ending with digit K, then the result will be the maximum of 10. Follow the steps below to solve the problem:
- If the digit K is even and the integer N is odd, then print “-1″ as it is not possible to obtain an odd sum with an even number.
- For the number K, find the smallest number ending with digit i over the range [0, 9]. If it is not possible, then set the value as INT_MAX.
- Also, for each number K find the minimum steps required to create a number that ends with digit i over the range [0, 9].
- Now, if the smallest number ending with digit i is greater than N with unit digit i then print “-1” as it is no possible to make the sum of numbers as N.
- Otherwise, the answer will be minimum steps to create a number that ends with digit i which is the same as the unit digit of N because the remaining digit can be obtained by inserting those digits in any of the numbers that contribute to the answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int minCount( int N, int K) { // Stores the smallest number that // ends with digit i (0, 9) int SmallestNumber[10]; // Stores the minimum number of // steps to create a number ending // with digit i int MinimumSteps[10]; // Initialize elements as infinity for ( int i = 0; i <= 9; i++) { SmallestNumber[i] = INT_MAX; MinimumSteps[i] = INT_MAX; } for ( int i = 1; i <= 10; i++) { int num = K * i; // Minimum number // ending with digit i SmallestNumber[num % 10] = min( SmallestNumber[num % 10], num); // Minimum steps to create a // number ending with digit i MinimumSteps[num % 10] = min( MinimumSteps[num % 10], i); } // If N < SmallestNumber then, // return -1 if (N < SmallestNumber[N % 10]) { return -1; } // Otherwise, return answer else { return MinimumSteps[N % 10]; } } // Driver Code int main() { int N = 42, K = 7; cout << minCount(N, K); return 0; } |
Java
// Java program for above approach import java.util.*; class GFG{ static int minCount( int N, int K) { // Stores the smallest number that // ends with digit i (0, 9) int SmallestNumber[] = new int [ 10 ]; // Stores the minimum number of // steps to create a number ending // with digit i int MinimumSteps[] = new int [ 10 ]; // Initialize elements as infinity for ( int i = 0 ; i <= 9 ; i++) { SmallestNumber[i] = Integer.MAX_VALUE; MinimumSteps[i] = Integer.MAX_VALUE; } for ( int i = 1 ; i <= 10 ; i++) { int num = K * i; // Minimum number // ending with digit i SmallestNumber[num % 10 ] = Math.min( SmallestNumber[num % 10 ], num); // Minimum steps to create a // number ending with digit i MinimumSteps[num % 10 ] = Math.min( MinimumSteps[num % 10 ], i); } // If N < SmallestNumber then, // return -1 if (N < SmallestNumber[N % 10 ]) { return - 1 ; } // Otherwise, return answer else { return MinimumSteps[N % 10 ]; } } // Driver Code public static void main(String[] args) { int N = 42 , K = 7 ; System.out.println(minCount(N, K)); } } // This code is contributed by hritikrommie |
Python3
# Python3 program for the above approach import sys def minCount(N, K): # Stores the smallest number that # ends with digit i (0, 9) SmallestNumber = [ 0 for i in range ( 10 )] # Stores the minimum number of # steps to create a number ending # with digit i MinimumSteps = [ 0 for i in range ( 10 )] # Initialize elements as infinity for i in range ( 10 ): SmallestNumber[i] = sys.maxsize; MinimumSteps[i] = sys.maxsize for i in range ( 1 , 11 , 1 ): num = K * i # Minimum number # ending with digit i SmallestNumber[num % 10 ] = min (SmallestNumber[num % 10 ],num) # Minimum steps to create a # number ending with digit i MinimumSteps[num % 10 ] = min (MinimumSteps[num % 10 ], i) # If N < SmallestNumber then, # return -1 if (N < SmallestNumber[N % 10 ]): return - 1 # Otherwise, return answer else : return MinimumSteps[N % 10 ] # Driver Code if __name__ = = '__main__' : N = 42 K = 7 print (minCount(N, K)) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; public class GFG{ static int minCount( int N, int K) { // Stores the smallest number that // ends with digit i (0, 9) int [] SmallestNumber = new int [10]; // Stores the minimum number of // steps to create a number ending // with digit i int [] MinimumSteps = new int [10]; // Initialize elements as infinity for ( int i = 0; i <= 9; i++) { SmallestNumber[i] = Int32.MaxValue; MinimumSteps[i] = Int32.MaxValue; } for ( int i = 1; i <= 10; i++) { int num = K * i; // Minimum number // ending with digit i SmallestNumber[num % 10] = Math.Min( SmallestNumber[num % 10], num); // Minimum steps to create a // number ending with digit i MinimumSteps[num % 10] = Math.Min( MinimumSteps[num % 10], i); } // If N < SmallestNumber then, // return -1 if (N < SmallestNumber[N % 10]) { return -1; } // Otherwise, return answer else { return MinimumSteps[N % 10]; } } // Driver Code static public void Main () { int N = 42, K = 7; Console.Write(minCount(N, K)); } } // This code is contributed by shubhamsingh10 |
Javascript
<script> // JavaScript program for the above approach function minCount(N, K) { // Stores the smallest number that // ends with digit i (0, 9) let SmallestNumber = new Array(10); // Stores the minimum number of // steps to create a number ending // with digit i let MinimumSteps = new Array(10); // Initialize elements as infinity for (let i = 0; i <= 9; i++) { SmallestNumber[i] = Number.MAX_VALUE; MinimumSteps[i] = Number.MAX_VALUE; } for (let i = 1; i <= 10; i++) { let num = K * i; // Minimum number // ending with digit i SmallestNumber[num % 10] = Math.min( SmallestNumber[num % 10], num); // Minimum steps to create a // number ending with digit i MinimumSteps[num % 10] = Math.min( MinimumSteps[num % 10], i); } // If N < SmallestNumber then, // return -1 if (N < SmallestNumber[N % 10]) { return -1; } // Otherwise, return answer else { return MinimumSteps[N % 10]; } } // Driver Code let N = 42, K = 7; document.write(minCount(N, K)); </script> |
6
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!