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.
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.
- 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
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Count of N-digit numbers having digit XOR as single digit
- Count elements in the given range which have maximum number of divisors
- Count the numbers < N which have equal number of divisors as K
- Prove that atleast one of three consecutive even numbers is divisible by 6
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Sub-strings that start and end with one character and have at least one other
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Find sum of divisors of all the divisors of a natural number
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count numbers from 1 to n that have 4 as a digit
- Count of numbers in range which are divisible by M and have digit D at odd places
Improved By : Mithun Kumar