Given integers N and K, the task is to check if a number can be represented as the sum of numbers that have at least one digit equal to K.
Example:
Input: N = 68, K = 7
Output: YES
Explanation: 68 = (27 + 17 + 17 + 7). Each number has atleast one digit equal to 7.Input: N = 23, K = 3
Output: YES
Explanation: 23 itself contains a digit equal to 3.
Approach: The given problem can be solved by using simple concepts of math. Follow the steps below to solve the problem:
- Initialize a variable temp to k, and also take a counter say count, assign it with 0
- Iterate till the last digits of temp and N are not equal and at each iteration
- Increment the value of temp by k
- Keep a count of iterations and break the loop if the count becomes greater than 10
- Check if the last digits of temp and N are equal, and if the value of temp <= N:
- If the above condition is satisfied return true
- Else return false
- Also if k * 10 <= N, return true
- Else return false
Below is the implementation of the above approach:
C++
// C++ implementation for the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to Check if a number can// be equal to sum of numbers having // at least one digit equal to kbool checkEqualtoSum(int N, int k){    // Temporary variable to    // store k    int temp = k;         // Variable for count    int count = 0;         // Iterating till count is less or    // equal to 10 and N % 10 is not    // equal to temp % 10    while(count <= 10 &&                   N % 10 != temp % 10) {               temp += k;        count++;    }         // If N % 10 is equal to temp % 10    // and temp is less or equal to N,     // return true    if(N % 10 == temp % 10 && temp <= N)        return true;         // If k * 10 <= N, return true    if(k * 10 <= N)        return true;         // Else return false    return false;}Â
// Driver Codeint main(){Â Â Â Â int N = 68;Â Â Â Â int K = 7;Â
      // Call the function    if(checkEqualtoSum(N, K))           cout << "YES";    else cout << "NO";         return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG {Â
    // Function to Check if a number can    // be equal to sum of numbers having    // at least one digit equal to k    static boolean checkEqualtoSum(int N, int k)    {        // Temporary variable to        // store k        int temp = k;Â
        // Variable for count        int count = 0;Â
        // Iterating till count is less or        // equal to 10 and N % 10 is not        // equal to temp % 10        while (count <= 10 && N % 10 != temp % 10) {Â
            temp += k;            count++;        }Â
        // If N % 10 is equal to temp % 10        // and temp is less or equal to N,        // return true        if (N % 10 == temp % 10 && temp <= N)            return true;Â
        // If k * 10 <= N, return true        if (k * 10 <= N)            return true;Â
        // Else return false        return false;    }Â
    // Driver Code    public static void main(String[] args)    {        // Given Input        int N = 68;        int K = 7;Â
        // Call the function        if (checkEqualtoSum(N, K))            System.out.println("YES");        else            System.out.println("NO");    }}Â
// This code is contributed by dwivediyash |
Python3
# python implementation for the above approachÂ
# Function to Check if a number can# be equal to sum of numbers having# at least one digit equal to kÂ
Â
def checkEqualtoSum(N, k):Â
    # Temporary variable to    # store k    temp = kÂ
    # Variable for count    count = 0Â
    # Iterating till count is less or    # equal to 10 and N % 10 is not    # equal to temp % 10    while(count <= 10 and N % 10 != temp % 10):Â
        temp += k        count += 1Â
    # If N % 10 is equal to temp % 10    # and temp is less or equal to N,    # return true    if(N % 10 == temp % 10 and temp <= N):        return TrueÂ
    # If k * 10 <= N, return true    if(k * 10 <= N):        return TrueÂ
    # Else return false    return FalseÂ
Â
# Driver Codeif __name__ == "__main__":Â
    N = 68    K = 7Â
    # Call the function    if(checkEqualtoSum(N, K)):        print("YES")    else:        print("NO")Â
    # This code is contributed by rakeshsahni |
Javascript
<script>Â Â Â Â // JavaScript implementation for the above approachÂ
    // Function to Check if a number can    // be equal to sum of numbers having    // at least one digit equal to k    const checkEqualtoSum = (N, k) => {        // Temporary variable to        // store k        let temp = k;Â
        // Variable for count        let count = 0;Â
        // Iterating till count is less or        // equal to 10 and N % 10 is not        // equal to temp % 10        while (count <= 10 &&            N % 10 != temp % 10) {Â
            temp += k;            count++;        }Â
        // If N % 10 is equal to temp % 10        // and temp is less or equal to N,        // return true        if (N % 10 == temp % 10 && temp <= N)            return true;Â
        // If k * 10 <= N, return true        if (k * 10 <= N)            return true;Â
        // Else return false        return false;    }Â
    // Driver Code    let N = 68;    let K = 7;Â
    // Call the function    if (checkEqualtoSum(N, K))        document.write("YES");    else document.write("NO");Â
    // This code is contributed by rakeshsahniÂ
</script> |
C#
// C# implementation for the above approachusing System;class gFG{       // Function to Check if a number can    // be equal to sum of numbers having    // at least one digit equal to k    static bool checkEqualtoSum(int N, int k)    {        // Temporary variable to        // store k        int temp = k;Â
        // Variable for count        int count = 0;Â
        // Iterating till count is less or        // equal to 10 and N % 10 is not        // equal to temp % 10        while (count <= 10 && N % 10 != temp % 10) {Â
            temp += k;            count++;        }Â
        // If N % 10 is equal to temp % 10        // and temp is less or equal to N,        // return true        if (N % 10 == temp % 10 && temp <= N)            return true;Â
        // If k * 10 <= N, return true        if (k * 10 <= N)            return true;Â
        // Else return false        return false;    }Â
    // Driver Code    public static void Main()    {        int N = 68;        int K = 7;Â
        // Call the function        if (checkEqualtoSum(N, K))            Console.WriteLine("YES");        else            Console.WriteLine("NO");    }}Â
// This code is contributed by ukasp. |
Â
Â
YES
Â
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!

… [Trackback]
[…] Find More on that Topic: geeksforgeeks.org/check-if-a-number-can-be-represented-as-the-sum-of-numbers-with-at-least-one-digit-equal-to-k/ […]
… [Trackback]
[…] Read More Information here to that Topic: geeksforgeeks.org/check-if-a-number-can-be-represented-as-the-sum-of-numbers-with-at-least-one-digit-equal-to-k/ […]