Minimum count of numbers required with unit digit X that sums up to N

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (int times = 1; times <= 10;
         times++) {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
int main()
{
    int N = 58, X = 7;
    cout << getNum(N, X) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement
// the above approach
class GFG{
      
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++) 
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else 
    {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
public static void main(String []args)
{
    int N = 58, X = 7;
    System.out.println( getNum(N, X));
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to implement
// the above approach
using System;
class GFG{
      
// Function to calculate and return
// the minimum number of times a number
// with unit digit X needs to be added
// to get a sum N
static int check(int unit_digit, int X)
{
    int times, digit;
  
    // Calculate the number of
    // additions required to get unit
    // digit of N
    for (times = 1; times <= 10;
                    times++) 
    {
        digit = (X * times) % 10;
        if (digit == unit_digit)
            return times;
    }
  
    // If unit digit of N
    // cannot be obtained
    return -1;
}
  
// Function to return the minimum
// number required to represent N
static int getNum(int N, int X)
{
    int unit_digit;
  
    // Stores unit digit of N
    unit_digit = N % 10;
  
    // Stores minimum addition
    // of X required to
    // obtain unit digit of N
    int times = check(unit_digit, X);
  
    // If unit digit of N
    // cannot be obtained
    if (times == -1)
        return times;
  
    // Otherwise
    else
    {
  
        // If N is greater than
        // or equal to (X*times)
        if (N >= (times * X))
  
            // Minimum count of numbers
            // that needed to represent N
            return times;
  
        // Representation not
        // possible
        else
            return -1;
    }
}
  
// Driver Code
public static void Main()
{
    int N = 58, X = 7;
    Console.Write(getNum(N, X));
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

4

Time Complexity: O(1)
Auxiliary Space: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : btc_148, Code_Mech