Self Dividing Numbers (LeetCode 728): Solution and Explanation
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
- Create an empty result list.
- For each
nfromlefttoright, copy it intox. - While
x > 0, readd = x % 10. Ifd == 0orn % d != 0, break. Otherwise drop the digit withx //= 10. - After the loop,
x == 0means no digit failed, so appendn. - Return the list, which is already in increasing order.
Step-by-step example
left = 1, right = 22
- 1 through 9: the only digit is n itself, and n % n = 0, so all nine are added.
- 10: the last digit is 0, so it fails at once.
- 11: 11 % 1 = 0 twice, so it is added. 12: 12 % 2 = 0 and 12 % 1 = 0, added.
- 13: 13 % 3 = 1, fails. 14: 14 % 4 = 2, fails. 15: 15 % 5 = 0 and 15 % 1 = 0, added.
- 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.
- 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 result1class Solution {
2 public List<Integer> selfDividingNumbers(int left, int right) {
3 List<Integer> result = new ArrayList<>();
4 for (int n = left; n <= right; n++) {
5 int x = n;
6 while (x > 0) {
7 int d = x % 10;
8 if (d == 0 || n % d != 0) break;
9 x /= 10;
10 }
11 if (x == 0) result.add(n);
12 }
13 return result;
14 }
15}1class Solution {
2public:
3 vector<int> selfDividingNumbers(int left, int right) {
4 vector<int> result;
5 for (int n = left; n <= right; n++) {
6 int x = n;
7 while (x > 0) {
8 int d = x % 10;
9 if (d == 0 || n % d != 0) break;
10 x /= 10;
11 }
12 if (x == 0) result.push_back(n);
13 }
14 return result;
15 }
16};Time and space complexity
O(R log R)
Each of the R = right - left + 1 numbers has O(log10 right) digits, and each digit costs one modulo.
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 % dbefore checkingd == 0, which divides by zero for numbers like 10 or 101. - Testing the digits against
x(the shrinking copy) instead of the originaln. - Peeling digits from
nitself, which destroys the value you need to append and divide by. - Forgetting that
rightis 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.