Self Dividing Numbers (LeetCode 728): Solution and Explanation

Easy
Math

A number is self-dividing when every one of its digits divides it and none of its digits is 0. Given left and right, return all self-dividing numbers in [left, right] in increasing order.

Intuition

The range holds at most 10,000 numbers and each has at most 5 digits, so testing every number directly is cheap. The only real question is how to look at the digits of a number.

Taking x % 10 gives the last digit, and x //= 10 removes it. Repeat until x is 0 and you have visited every digit from right to left, without building a string.

Each digit gets two checks: it must not be 0 (division by zero, and 0 is banned anyway), and n % d must be 0. The first failing digit decides the number, so you can stop early. If the loop finishes because x reached 0, every digit passed.

Approach: Digit peeling

  1. Create an empty result list.
  2. For each n from left to right, copy it into x.
  3. While x > 0, read d = x % 10. If d == 0 or n % d != 0, break. Otherwise drop the digit with x //= 10.
  4. After the loop, x == 0 means no digit failed, so append n.
  5. Return the list, which is already in increasing order.

Step-by-step example

left = 1, right = 22
  1. 1 through 9: the only digit is n itself, and n % n = 0, so all nine are added.
  2. 10: the last digit is 0, so it fails at once.
  3. 11: 11 % 1 = 0 twice, so it is added. 12: 12 % 2 = 0 and 12 % 1 = 0, added.
  4. 13: 13 % 3 = 1, fails. 14: 14 % 4 = 2, fails. 15: 15 % 5 = 0 and 15 % 1 = 0, added.
  5. 16 to 21 each fail on a digit (16 % 6, 17 % 7, 18 % 8, 19 % 9, the 0 in 20, 21 % 2 for the 2 in 21). 22: 22 % 2 = 0 twice, added.
  6. Return [1,2,3,4,5,6,7,8,9,11,12,15,22].

Code: Python, Java, C++

1class Solution:
2    def selfDividingNumbers(self, left: int, right: int) -> List[int]:
3        result = []
4        for n in range(left, right + 1):
5            x = n
6            while x > 0:
7                d = x % 10
8                if d == 0 or n % d != 0:
9                    break
10                x //= 10
11            if x == 0:
12                result.append(n)
13        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(R log R)

Each of the R = right - left + 1 numbers has O(log10 right) digits, and each digit costs one modulo.

Space

O(1)

Apart from the returned list, only a few integers are stored.

Other approaches

String digits (time O(R log R), space O(log R))

Convert n to a string and loop over its characters, converting each back to an integer. Easy to read, but it allocates a string per number.

Precompute all answers (time O(10^4 log 10^4) once, then O(answer) per query, space O(10^4))

Since the range is capped at 10,000, you could build the full list of self-dividing numbers once and slice it per query. This pays off only when many queries are asked.

Common mistakes

  • Computing n % d before checking d == 0, which divides by zero for numbers like 10 or 101.
  • Testing the digits against x (the shrinking copy) instead of the original n.
  • Peeling digits from n itself, which destroys the value you need to append and divide by.
  • Forgetting that right is inclusive.

Frequently asked questions

Why is 0 not allowed as a digit?

No number is divisible by 0, so a number containing 0 can never pass the digit test. The problem states the rule explicitly so you know to reject it rather than crash.

Are single-digit numbers always self-dividing?

Yes. 1 through 9 each have one digit equal to themselves, and every number divides itself.

Can I stop at the first failing digit?

Yes. One failing digit is enough to reject the number, so breaking early saves work and is what the loop does.