Given an number n, find the number of divisors whose at least one digit in the decimal representation matches with the number n.
Input : n = 10 Output: 2 Explanation: numbers are 1 and 10, 1 and 10 both have a nimbus of 1 digit common with n = 10. Input : n = 15 Output: 3 Explanation: the numbers are 1, 5, 15, all of them have a minimum of 1 digit common.
A naive approach is to iterate from 1 to n and check for all the divisors, and use hashing to determine if any of the digits match with n or not.
Time complexity: O(n)
Auxiliary space: O(1)
An efficient approach is to iterate from 1 to sqrt(n) and check for all the divisors i and n/i, if both are different, we check if there is any match for i and n/i, if yes we simply add 1 to the answer. We use hashing to store if a number is present or not.
Below is the implementation of the above approach
Time complexity: O(sqrt n)
Auxiliary Space: O(1)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Common Divisors of Two Numbers
- Common divisors of N numbers
- Sum of common divisors of two numbers A and B
- C++ Program for Common Divisors of Two Numbers
- Divide the two given numbers by their common divisors
- Java Program for Common Divisors of Two Numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count 'd' digit positive integers with 0 as a digit
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Count n digit numbers not having a particular digit
- Count Divisors of n in O(n^1/3)
Improved By : Mithun Kumar