A self-dividing number is a number that is divisible by every digit it contains.
128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.A self-dividing number is not allowed to contain the digit zero.
Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right] (both inclusive).
Example 1:
Input: left = 1, right = 22 Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
Example 2:
Input: left = 47, right = 85 Output: [48,55,66,77]
Constraints:
1 <= left <= right <= 104When 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 strategy involves examining every number within a given range to determine if it's self-dividing. For each number, we will check if every digit divides the number evenly.
Here's how the algorithm would work step-by-step:
def self_dividing_numbers(left_number, right_number):
self_dividing_number_list = []
for number_to_check in range(left_number, right_number + 1):
is_self_dividing = True
number_string = str(number_to_check)
for digit_character in number_string:
digit = int(digit_character)
# Exclude numbers with 0 as a digit as division by 0 is not allowed
if digit == 0:
is_self_dividing = False
break
# If the number is not divisible by the digit, it's not self dividing
if number_to_check % digit != 0:
is_self_dividing = False
break
# We only append if all digits divided the number
if is_self_dividing:
self_dividing_number_list.append(number_to_check)
return self_dividing_number_listThe goal is to find numbers where each digit divides the number evenly. The best way to do this is to check each number digit by digit, skipping numbers that have a zero as a digit because zero can never be a divisor.
Here's how the algorithm would work step-by-step:
def self_dividing_numbers(left, right):
result = []
for number in range(left, right + 1):
number_string = str(number)
is_self_dividing = True
for digit_char in number_string:
digit = int(digit_char)
# Numbers with zero cannot be self-dividing
if digit == 0:
is_self_dividing = False
break
# Check if the number is divisible by the digit
if number % digit != 0:
is_self_dividing = False
break
# If it is self dividing, then add to result
if is_self_dividing:
result.append(number)
return result| Case | How to Handle |
|---|---|
| Left boundary greater than right boundary | Return an empty list if left > right as it represents an invalid range. |
| Input range contains 0 | Numbers containing 0 are not self-dividing, so the solution must skip them during the self-dividing check. |
| Single digit numbers | Single-digit numbers should be included if they are self-dividing (i.e., 1-9). |
| Numbers with repeated digits, e.g., 11, 22, 33 | The self-dividing check must correctly handle repeated digits when performing the modulo operation. |
| Large input range (e.g., 1 to 10000) | The solution should iterate efficiently through the range to avoid exceeding time limits. |
| Numbers that are divisible by their digits but the digits are not unique | The self-dividing check needs to handle repeating digits, for example, 122, which is divisible by 1 and 2. |
| The range only contains a single number | The code should check if the single number is self dividing and return a list containing that number or an empty list. |
| Integer overflow when number is very large | Since the input is an integer range, overflow isn't likely, but should be considered by limiting the maximum input number. |