Skip to content
Related Articles

Related Articles

Count of numbers from the range [L, R] which contains at least one digit that divides K
  • Last Updated : 20 Dec, 2019

Given three positive integers L, R and K.The task is to find the count of all the numbers from the range [L, R] that contains at least one digit which divides the number K.

Examples:

Input: L = 5, R = 11, K = 10
Output: 3
5, 10 and 11 are only such numbers.

Input: L = 32, R = 38, K = 13
Output: 0

Approach: Initialise count = 0 and for every element in the range [L, R], check if it contains at least one digit that divides K. If yes then increment the count.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if num
// contains at least one digit
// that divides k
bool digitDividesK(int num, int k)
{
    while (num) {
  
        // Get the last digit
        int d = num % 10;
  
        // If the digit is non-zero
        // and it divides k
        if (d != 0 and k % d == 0)
            return true;
  
        // Remove the last digit
        num = num / 10;
    }
  
    // There is no digit in num
    // that divides k
    return false;
}
  
// Function to return the required
// count of elements from the given
// range which contain at least one
// digit that divides k
int findCount(int l, int r, int k)
{
  
    // To store the result
    int count = 0;
  
    // For every number from the range
    for (int i = l; i <= r; i++) {
  
        // If any digit of the current
        // number divides k
        if (digitDividesK(i, k))
            count++;
    }
    return count;
}
  
// Driver code
int main()
{
    int l = 20, r = 35;
    int k = 45;
  
    cout << findCount(l, r, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
  
class GFG
{
    // Function that returns true if num 
    // contains at least one digit 
    // that divides k 
    static boolean digitDividesK(int num, int k) 
    
        while (num != 0
        
      
            // Get the last digit 
            int d = num % 10
      
            // If the digit is non-zero 
            // and it divides k 
            if (d != 0 && k % d == 0
                return true
      
            // Remove the last digit 
            num = num / 10
        
      
        // There is no digit in num 
        // that divides k 
        return false
    
      
    // Function to return the required 
    // count of elements from the given 
    // range which contain at least one 
    // digit that divides k 
    static int findCount(int l, int r, int k) 
    
      
        // To store the result 
        int count = 0
      
        // For every number from the range 
        for (int i = l; i <= r; i++) 
        
      
            // If any digit of the current 
            // number divides k 
            if (digitDividesK(i, k)) 
                count++; 
        
        return count; 
    
      
    // Driver code 
    public static void main(String []args)
    
        int l = 20, r = 35
        int k = 45
      
        System.out.println(findCount(l, r, k)); 
    
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that returns true if num
# contains at least one digit
# that divides k
def digitDividesK(num, k):
    while (num):
  
        # Get the last digit
        d = num % 10
  
        # If the digit is non-zero
        # and it divides k
        if (d != 0 and k % d == 0):
            return True
  
        # Remove the last digit
        num = num // 10
  
    # There is no digit in num
    # that divides k
    return False
  
# Function to return the required
# count of elements from the given
# range which contain at least one
# digit that divides k
def findCount(l, r, k):
  
    # To store the result
    count = 0
  
    # For every number from the range
    for i in range(l, r + 1):
  
        # If any digit of the current
        # number divides k
        if (digitDividesK(i, k)):
            count += 1
  
    return count
  
# Driver code
l = 20
r = 35
k = 45
  
print(findCount(l, r, k))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
    // Function that returns true if num 
    // contains at least one digit 
    // that divides k 
    static bool digitDividesK(int num, int k) 
    
        while (num != 0) 
        
      
            // Get the last digit 
            int d = num % 10; 
      
            // If the digit is non-zero 
            // and it divides k 
            if (d != 0 && k % d == 0) 
                return true
      
            // Remove the last digit 
            num = num / 10; 
        
      
        // There is no digit in num 
        // that divides k 
        return false
    
      
    // Function to return the required 
    // count of elements from the given 
    // range which contain at least one 
    // digit that divides k 
    static int findCount(int l, int r, int k) 
    
      
        // To store the result 
        int count = 0; 
      
        // For every number from the range 
        for (int i = l; i <= r; i++) 
        
      
            // If any digit of the current 
            // number divides k 
            if (digitDividesK(i, k)) 
                count++; 
        
        return count; 
    
      
    // Driver code 
    public static void Main()
    
        int l = 20, r = 35; 
        int k = 45; 
      
        Console.WriteLine(findCount(l, r, k)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :