You are given an integer num
. Your task is to return the number of digits in num
that divide num
.
An integer val
is said to divide num
if num % val == 0
.
Example 1:
Input: num = 7
Output: 1
Explanation: 7 divides itself, hence the answer is 1.
Example 2:
Input: num = 121
Output: 2
Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
Example 3:
Input: num = 1248
Output: 4
Explanation: 1248 is divisible by all of its digits, hence the answer is 4.
Constraints:
1 <= num <= 10^9
num
does not contain 0
as one of its digits.Could you implement a function to solve this problem efficiently?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method here means checking every single digit of the number and testing it to see if it divides evenly into the original number. We'll go through each digit one by one and perform a division. We then count how many divisions result in a whole number with no remainder.
Here's how the algorithm would work step-by-step:
def count_digits_that_divide(number):
number_string = str(number)
count = 0
for digit_char in number_string:
digit = int(digit_char)
# Avoid division by zero
if digit == 0:
continue
# Checking if the digit divides evenly into the original number
if number % digit == 0:
# Increment the count if it divides evenly
count += 1
return count
The goal is to count how many digits of a number evenly divide the number itself. We achieve this by examining each digit individually and checking if it's a divisor.
Here's how the algorithm would work step-by-step:
def count_the_digits_that_divide_a_number(number):
original_number = number
number_as_string = str(number)
count = 0
for digit_character in number_as_string:
digit = int(digit_character)
# Avoid division by zero
if digit == 0:
continue
# Ensure the original number is divisible by the digit
if original_number % digit == 0:
count += 1
return count
Case | How to Handle |
---|---|
Input number is 0 | Handle the zero case separately to avoid division by zero errors by returning 0. |
Input number is a single-digit number | The digit divides the number if it is not zero, and the number is not zero. |
Input number contains the digit 0 | The code should avoid division by zero when checking if the digit divides the number. |
Input number is a very large number (potential overflow) | Ensure the algorithm uses a data type that can accommodate the number without overflow; consider using strings. |
All digits divide the number | The solution should correctly count all the digits in this scenario. |
No digits divide the number | The solution should return 0 in this case. |
Negative input numbers | The problem statement does not mention anything about negative inputs; so, one can assume we only handle positive integers, or clarify this assumption with the interviewer, or take the absolute value of the number. |
Number with repeated digits, some of which divide and some do not | The solution needs to ensure each digit occurrence is checked only once and counted accordingly. |