Count of minimum numbers having K as the last digit required to obtain sum N
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++
#include <bits/stdc++.h>
using namespace std;
int minCount( int N, int K)
{
int SmallestNumber[10];
int MinimumSteps[10];
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;
SmallestNumber[num % 10]
= min(
SmallestNumber[num % 10],
num);
MinimumSteps[num % 10]
= min(
MinimumSteps[num % 10], i);
}
if (N < SmallestNumber[N % 10]) {
return -1;
}
else {
return MinimumSteps[N % 10];
}
}
int main()
{
int N = 42, K = 7;
cout << minCount(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minCount( int N, int K)
{
int SmallestNumber[] = new int [ 10 ];
int MinimumSteps[] = new int [ 10 ];
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;
SmallestNumber[num % 10 ] = Math.min(
SmallestNumber[num % 10 ], num);
MinimumSteps[num % 10 ] = Math.min(
MinimumSteps[num % 10 ], i);
}
if (N < SmallestNumber[N % 10 ])
{
return - 1 ;
}
else
{
return MinimumSteps[N % 10 ];
}
}
public static void main(String[] args)
{
int N = 42 , K = 7 ;
System.out.println(minCount(N, K));
}
}
|
Python3
import sys
def minCount(N, K):
SmallestNumber = [ 0 for i in range ( 10 )]
MinimumSteps = [ 0 for i in range ( 10 )]
for i in range ( 10 ):
SmallestNumber[i] = sys.maxsize;
MinimumSteps[i] = sys.maxsize
for i in range ( 1 , 11 , 1 ):
num = K * i
SmallestNumber[num % 10 ] = min (SmallestNumber[num % 10 ],num)
MinimumSteps[num % 10 ] = min (MinimumSteps[num % 10 ], i)
if (N < SmallestNumber[N % 10 ]):
return - 1
else :
return MinimumSteps[N % 10 ]
if __name__ = = '__main__' :
N = 42
K = 7
print (minCount(N, K))
|
C#
using System;
public class GFG{
static int minCount( int N, int K)
{
int [] SmallestNumber = new int [10];
int [] MinimumSteps = new int [10];
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;
SmallestNumber[num % 10] = Math.Min(
SmallestNumber[num % 10], num);
MinimumSteps[num % 10] = Math.Min(
MinimumSteps[num % 10], i);
}
if (N < SmallestNumber[N % 10])
{
return -1;
}
else
{
return MinimumSteps[N % 10];
}
}
static public void Main ()
{
int N = 42, K = 7;
Console.Write(minCount(N, K));
}
}
|
Javascript
<script>
function minCount(N, K)
{
let SmallestNumber = new Array(10);
let MinimumSteps = new Array(10);
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;
SmallestNumber[num % 10]
= Math.min(
SmallestNumber[num % 10],
num);
MinimumSteps[num % 10]
= Math.min(
MinimumSteps[num % 10], i);
}
if (N < SmallestNumber[N % 10]) {
return -1;
}
else {
return MinimumSteps[N % 10];
}
}
let N = 42, K = 7;
document.write(minCount(N, K));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...