Self Dividing Numbers - Problem

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because:

  • 128 % 1 == 0
  • 128 % 2 == 0
  • 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).

Input & Output

Example 1 — Basic Range
$ Input: left = 1, right = 22
Output: [1,2,3,4,5,6,7,8,9,11,12,15,22]
💡 Note: Numbers like 10, 20 contain zero (invalid). 13: 13%3=1≠0 (invalid). 22: 22%2=0 ✓ (valid)
Example 2 — Three-Digit Numbers
$ Input: left = 47, right = 85
Output: [48,55,66,77]
💡 Note: 48: 48%4=0, 48%8=0 ✓. 55: 55%5=0 ✓. 66: 66%6=0 ✓. 77: 77%7=0 ✓
Example 3 — Single Number Range
$ Input: left = 128, right = 128
Output: [128]
💡 Note: 128: 128%1=0, 128%2=0, 128%8=0, all digits divide evenly

Constraints

  • 1 ≤ left ≤ right ≤ 104

Visualization

Tap to expand
Self-Dividing Numbers: Range [1, 22]Range [1,22]Input RangeCheck Each Number:✓ 1,2,3,4,5,6,7,8,9 (single digits always work)✗ 10,20 (contain zero) ✓ 11,12 (11%1=0, 12%1=0,12%2=0)✗ 13,14 (13%3≠0, 14%4≠0) ✓ 15,22 (15%1=0,15%5=0, 22%2=0)Divisibility Testing[1,2,3,4,5,6,7,8,9,11,12,15,22]Valid NumbersAlgorithm Steps:1. Iterate through range [1, 22]2. For each number, extract digits and check divisibility3. Skip numbers with zero, add valid ones to result
Understanding the Visualization
1
Input Range
Given range [left, right] to search
2
Check Each Number
Test divisibility by each digit
3
Collect Valid Numbers
Return list of self-dividing numbers
Key Takeaway
🎯 Key Insight: Numbers with zero digits are automatically invalid due to division by zero
Asked in
Amazon 15 Google 12 Microsoft 8
23.4K Views
Medium Frequency
~15 min Avg. Time
986 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