Open In App

Count of minimum numbers having K as the last digit required to obtain sum N

Last Updated : 28 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output: 

6

 

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



Similar Reads

Minimum N-Digit number required to obtain largest N-digit number after performing given operations
Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number: Convert the number to its Binary Coded Decimal form.Concatenate all the resulting nibbles to form a binary number.Remove the least significant N bits from the
6 min read
Count numbers in a range with digit sum divisible by K having first and last digit different
Given a range in the form of L and R, and a value K, the task is to count the numbers between the range L to R such that the sum of the digit is divisible by K, and the first digit is not equal to the last digit of the number. Examples: Input: L = 1, R = 10, K = 2 Output: 0 Explanation: The input numbers whose sum of the digits are divisible by 2 (
15 min read
Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
14 min read
Minimum number of coins having value equal to powers of 2 required to obtain N
Given an integer N, the task is to find the minimum number of coins of the form 2i required to make a change for N cents. Examples: Input: N = 5 Output: 2 Explanation: Possible values of coins are: {1, 2, 4, 8, ...} Possible ways to make change for N cents are as follows: 5 = 1 + 1 + 1 + 1 + 1 5 = 1 + 2 + 2 5 = 1 + 4 (Minimum) Therefore, the requir
4 min read
Minimum Deci-Binary numbers required to obtain a given sum S
Given a numeric string S representing a positive decimal integer, the task is to find the minimum number of positive Deci-Binary numbers required to obtain the sum S. Deci-Binary Numbers: Decimal numbers consisting of only 0s and 1s as its digits. Examples: Input: S = "31"Output: 3Explanation: S can be represented as the sum of minimum of 3 Deci-Bi
5 min read
Minimum count of digits required to obtain given Sum
Given an integer N, the task is to find the minimum number of digits required to generate a number having the sum of digits equal to N. Examples: Input: N = 18 Output: 2 Explanation: The number with smallest number of digits having sum of digits equal to 18 is 99. Input: N = 28 Output: 4 Explanation: 4-digit numbers like 8884, 6877, etc are the sma
3 min read
Last digit of a number raised to last digit of N factorial
Given two number X and N, the task is to find the last digit of X raised to last digit of N factorial, i.e. [Tex]X^{\left ( N! \right )mod 10} [/Tex].Examples: Input: X = 5, N = 2 Output: 5 Explanation: Since, 2! mod 10 = 2 therefore 52 = 25 and the last digit of 25 is 5.Input: X = 10, N = 4 Output: 0 Explanation: Since, 4! mod 10 = 24 mod 10 = 4 t
15 min read
Count of Numbers in Range where first digit is equal to last digit of the number
Given a range represented by two positive integers L and R. Find the count of numbers in the range where the first digit is equal to the last digit of the number. Examples: Input : L = 2, R = 60 Output : 13 Explanation : Required numbers are 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44 and 55 Input : L = 1, R = 1000 Output : 108 Prerequisites: Digit DPTh
16 min read
Minimum count of elements required to obtain the given Array by repeated mirror operations
Given an array arr[] consisting of N integers, the task is to find the array K[] of minimum possible length such that after performing multiple mirror operations on K[], the given array arr[] can be obtained. Mirror Operation: Appending all the array elements to the original array in reverse order. Illustration: arr[] = {1, 2, 3} After 1st mirror o
7 min read
Count all numbers up to N having M as the last digit
Given two positive numbers M and N, the task is to find the count of all numbers having M as the last digit from the range [1, N]. Examples: Input: M = 5, N = 15 Output: 2 Explanation: Only 2 numbers(5 and 15) from the range [1, 15] ends with the digit '5'.Input: M = 1, N = 100 Output: 10 Naive Approach: The simplest approach is to iterate over the
5 min read