Given three integers N, A and B, the task is to find whether N is divisible by any number that contains only A and B as it’s digits.
Input: N = 106, a = 3, b = 5
106 is divisible by 53
Input: N = 107, a = 3, b = 5
Approach 1 (Recursive): An efficient solution is to make all the numbers that contains a and b as their digits using recursive function starting with the numbers a and b. If function call is fun(x) then recursively call for fun(x * 10 + a) and fun(x * 10 + b). If n is divisible by any of the number then print Yes else print No.
Below is the implementation of the above approach:
Approach 2 (Queue Based): The idea is to generate all numbers (smaller than n) containing digits a and b. For every number check if it divides n or not. How to generate all numbers smaller than n? We use queue for this. Initially we push ‘a’ and ‘b’ to the queue. Then we run a loop while front of queue is smaller than n. We pop an item one by one and for ever popped item x, we generate next numbers x*10 + a and x*10 + b and enqueue them. Time complexity of this approach is O(n)
Please refer below post for implementation of this approach.
Count of Binary Digit numbers smaller than N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Program to check if a number is divisible by sum of its digits
- Program to check if a number is divisible by any of its digits
- Check whether sum of digits at odd places of a number is divisible by K
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Check whether product of digits at even places of a number is divisible by K
- Given a large number, check if a subsequence of digits is divisible by 8
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find N digits number which is divisible by D
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number of digits to be removed to make a number divisible by 3
- Possible to make a divisible by 3 number using all digits in an array
- Check if a number is divisible by 41 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.