Sequential Digits - Problem

An integer has sequential digits if and only if each digit in the number is one more than the previous digit.

Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.

Example: The number 234 has sequential digits because 2→3→4 where each digit is exactly one more than the previous. The number 235 does NOT have sequential digits because 3→5 skips 4.

Input & Output

Example 1 — Basic Range
$ Input: low = 100, high = 300
Output: [123,234]
💡 Note: 123 has sequential digits (1→2→3), 234 has sequential digits (2→3→4). No other numbers in range [100,300] have sequential digits.
Example 2 — Wider Range
$ Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
💡 Note: All 4-digit sequential numbers (1234-6789) and the 5-digit number 12345 fall within this range.
Example 3 — Single Digits
$ Input: low = 1, high = 10
Output: [1,2,3,4,5,6,7,8,9]
💡 Note: All single digits from 1-9 have sequential digits by definition (only one digit).

Constraints

  • 10 ≤ low ≤ high ≤ 109

Visualization

Tap to expand
Sequential Digits Problem INPUT Find sequential digit numbers in range [low, high] 100 (low) 300 (high) Sequential Digits: Each digit = previous + 1 1 2 3 OK 2 3 4 OK Input Values: low = 100, high = 300 ALGORITHM STEPS 1 Generate All Sequences Start with digits 1-9 2 Build Numbers Append next digit repeatedly Generation Process: 1 --> 12 --> 123 --> 1234... 2 --> 23 --> 234 --> 2345... 3 --> 34 --> 345 --> 3456... 3 Filter by Range Keep if low <= num <= high 4 Sort Results Return sorted list 12 < 100 (skip) 123 in [100,300] (keep) 234 in [100,300] (keep) FINAL RESULT Sequential numbers found: 123 1 +1 2 +1 3 234 2 +1 3 +1 4 Output: [123, 234] 2 sequential numbers found Key Insight: There are only 36 possible sequential digit numbers (from 12 to 123456789). We can generate all of them by starting with each digit 1-9 and repeatedly appending the next digit. Then filter by the given range. Time: O(1) since the total count is constant. Space: O(1) for the result. TutorialsPoint - Sequential Digits | Generate Sequential Numbers Approach
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
892 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