Self Dividing Numbers - Problem

A self-dividing number is a fascinating mathematical concept where a number is perfectly divisible by each of its individual digits. Think of it as a number that "gets along" with all its digits!

For example, 128 is self-dividing because:

  • 128 ÷ 1 = 128 (remainder 0)
  • 128 ÷ 2 = 64 (remainder 0)
  • 128 ÷ 8 = 16 (remainder 0)

Important: Self-dividing numbers cannot contain the digit 0 since division by zero is undefined.

Goal: Given a range [left, right], find all self-dividing numbers within this range (inclusive).

Input & Output

example_1.py — Basic Range
$ Input: left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
💡 Note: All single-digit numbers (1-9) are self-dividing. 11: 11÷1=11, 11÷1=11. 12: 12÷1=12, 12÷2=6. 15: 15÷1=15, 15÷5=3. 22: 22÷2=11, 22÷2=11.
example_2.py — Larger Range
$ Input: left = 47, right = 85
Output: [48, 55, 66, 77]
💡 Note: 48: 48÷4=12, 48÷8=6. 55: 55÷5=11, 55÷5=11. 66: 66÷6=11, 66÷6=11. 77: 77÷7=11, 77÷7=11.
example_3.py — Edge Case
$ Input: left = 1, right = 1
Output: [1]
💡 Note: Single number range. 1 is self-dividing because 1÷1=1.

Visualization

Tap to expand
Self-Dividing Number DetectionNumber: 128128128 ÷ 1 = 128 ✓128 ÷ 2 = 64 ✓128 ÷ 8 = 16 ✓🏆 Self-Dividing!
Understanding the Visualization
1
Meet the Team
Extract each digit (team member) from the number (captain)
2
Check for Troublemakers
If any digit is 0, it's a troublemaker (division by zero) - reject immediately
3
Test Team Chemistry
Check if the captain (original number) works harmoniously with each member (divisible by each digit)
4
Award the Medal
If all tests pass, the number earns the 'Self-Dividing' medal and joins our result team!
Key Takeaway
🎯 Key Insight: Every number is a potential team captain - we just need to check if it has good chemistry with all its digit teammates!

Time & Space Complexity

Time Complexity
⏱️
O(n * d)

Where n is the range size and d is the average number of digits per number

n
2n
Linear Growth
Space Complexity
O(1)

Only using constant extra space for digit checking

n
2n
Linear Space

Constraints

  • 1 ≤ left ≤ right ≤ 104
  • The range size will not exceed 10,000 numbers
  • No number containing digit 0 can be self-dividing
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
28.4K Views
Medium Frequency
~8 min Avg. Time
980 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen