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.
- Sum of common divisors of two numbers A and B
- Common Divisors of Two Numbers
- 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 'd' digit positive integers with 0 as a digit
- Longest subarray such that adjacent elements have at least one common digit
- Count n digit numbers not having a particular digit
- Count Divisors of n in O(n^1/3)
- Check if count of divisors is even or odd
- Count Divisors of Factorial
- Count total divisors of A or B in a given range
- Count all perfect divisors of a number
- Count divisors of array multiplication