Minimum count of numbers required with unit digit X that sums up to N
Last Updated :
11 May, 2021
Given two integers N and X, the task is to find the minimum count of integers with sum N and having unit digit X. If no such representation exists then print -1.
Examples:
Input: N = 38, X = 9
Output: 2
Explanation:
Minimum two integers are required with unit digit as X to represent as a sum equal to 38.
38 = 19 + 19 or 38 = 29 + 9
Input: N = 6, X = 4
Output: -1
Explanation:
No such representation of
Approach:
Follow the steps below to solve the problem:
- Obtain the unit digit of N and check if it achieved by the sum of numbers whose unit digits are X.
- If it is possible, check if N ? X * ( Minimum number of times a number with unit digit X needs to be added to get sum N).
- If the above condition is satisfied then print the minimum number of times a number with unit digit X needs to be added to get sum N. Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check( int unit_digit, int X)
{
int times, digit;
for ( int times = 1; times <= 10;
times++) {
digit = (X * times) % 10;
if (digit == unit_digit)
return times;
}
return -1;
}
int getNum( int N, int X)
{
int unit_digit;
unit_digit = N % 10;
int times = check(unit_digit, X);
if (times == -1)
return times;
else {
if (N >= (times * X))
return times;
else
return -1;
}
}
int main()
{
int N = 58, X = 7;
cout << getNum(N, X) << endl;
return 0;
}
|
Java
class GFG{
static int check( int unit_digit, int X)
{
int times, digit;
for (times = 1 ; times <= 10 ;
times++)
{
digit = (X * times) % 10 ;
if (digit == unit_digit)
return times;
}
return - 1 ;
}
static int getNum( int N, int X)
{
int unit_digit;
unit_digit = N % 10 ;
int times = check(unit_digit, X);
if (times == - 1 )
return times;
else
{
if (N >= (times * X))
return times;
else
return - 1 ;
}
}
public static void main(String []args)
{
int N = 58 , X = 7 ;
System.out.println( getNum(N, X));
}
}
|
Python3
def check(unit_digit, X):
for times in range ( 1 , 11 ):
digit = (X * times) % 10
if (digit = = unit_digit):
return times
return - 1
def getNum(N, X):
unit_digit = N % 10
times = check(unit_digit, X)
if (times = = - 1 ):
return times
else :
if (N > = (times * X)):
return times
else :
return - 1
N = 58
X = 7
print (getNum(N, X))
|
C#
using System;
class GFG{
static int check( int unit_digit, int X)
{
int times, digit;
for (times = 1; times <= 10;
times++)
{
digit = (X * times) % 10;
if (digit == unit_digit)
return times;
}
return -1;
}
static int getNum( int N, int X)
{
int unit_digit;
unit_digit = N % 10;
int times = check(unit_digit, X);
if (times == -1)
return times;
else
{
if (N >= (times * X))
return times;
else
return -1;
}
}
public static void Main()
{
int N = 58, X = 7;
Console.Write(getNum(N, X));
}
}
|
Javascript
<script>
function check(unit_digit, X)
{
let times, digit;
for (times = 1; times <= 10;
times++)
{
digit = (X * times) % 10;
if (digit == unit_digit)
return times;
}
return -1;
}
function getNum(N, X)
{
let unit_digit;
unit_digit = N % 10;
let times = check(unit_digit, X);
if (times == -1)
return times;
else
{
if (N >= (times * X))
return times;
else
return -1;
}
}
let N = 58, X = 7;
document.write( getNum(N, X));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...