Stepping Numbers - Problem

A stepping number is an integer such that all of its adjacent digits have an absolute difference of exactly 1.

For example, 321 is a stepping number while 421 is not.

Given two integers low and high, return a sorted list of all the stepping numbers in the inclusive range [low, high].

Input & Output

Example 1 — Basic Range
$ Input: low = 0, high = 21
Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
💡 Note: All single digits 0-9 are stepping numbers. For two digits: 10 (|1-0|=1), 12 (|1-2|=1), and 21 (|2-1|=1) are valid. Numbers like 11, 13, 14, etc. are invalid as adjacent digits don't differ by 1.
Example 2 — Higher Range
$ Input: low = 10, high = 15
Output: [10,12]
💡 Note: In range [10,15]: 10 has digits 1,0 with |1-0|=1 ✓. 12 has digits 1,2 with |1-2|=1 ✓. Numbers 11,13,14,15 don't satisfy the stepping property.
Example 3 — Single Range
$ Input: low = 90, high = 101
Output: [101]
💡 Note: Only 101 is a stepping number in this range: digits 1,0,1 have differences |1-0|=1 and |0-1|=1, both equal to 1.

Constraints

  • 0 ≤ low ≤ high ≤ 2 × 109

Visualization

Tap to expand
Stepping Numbers: Adjacent Digits Differ by Exactly 1Input Range: [0, 21]Stepping Property Check101211211323|1-0|=1✓|1-2|=1✓|1-1|=0✗|2-1|=1✓|1-3|=2✗|2-3|=1✓Process: Check each number in range for stepping propertyRule: All adjacent digits must have absolute difference = 1012...1012Output: [0,1,2,3,4,5,6,7,8,9,10,12,21]
Understanding the Visualization
1
Input Range
Given range [0, 21] to find stepping numbers
2
Check Property
Adjacent digits must differ by exactly 1
3
Valid Numbers
Collect all numbers satisfying the property
Key Takeaway
🎯 Key Insight: Generate stepping numbers by extending valid prefixes rather than checking all numbers
Asked in
Google 45 Amazon 35 Microsoft 28
32.4K Views
Medium Frequency
~25 min Avg. Time
856 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