Finding sequential digit numbers within a range in JavaScript

A number has sequential digits if and only if each digit in the number is one more than the previous digit. For example, 1234, 2345, and 6789 are sequential digit numbers, while 1324 and 2468 are not.

Problem Statement

We need to write a JavaScript function that takes an array of two elements specifying a range and returns a sorted array of all integers within that range (inclusive) that have sequential digits.

For example, if the input is:

const arr = [1000, 13000];

The output should be:

[1234, 2345, 3456, 4567, 5678, 6789, 12345]

Solution Approach

The algorithm works by:

  1. Finding the digit count of the low and high range boundaries
  2. Generating all possible sequential digit numbers with digit counts between these boundaries
  3. Filtering numbers that fall within the specified range

Implementation

const arr = [1000, 13000];

const sequentialDigits = ([low, high] = [1, 1]) => {
   // Helper function to count digits in a number
   const findCount = (num) => {
      let count = 0;
      while(num > 0){
         count += 1
         num = Math.floor(num / 10)
      };
      return count;
   };
   
   // Helper function to generate sequential digit number
   const helper = (count, start) => {
      let res = start;
      while(count > 1 && start < 9){
         res = res * 10 + start + 1;
         start += 1;
         count -= 1;
      };
      if(count > 1){
         return 0; // Cannot create more digits
      };
      return res;
   };
   
   const count1 = findCount(low);
   const count2 = findCount(high);
   const res = [];
   
   // Generate all sequential numbers with digit counts from count1 to count2
   for(let i = count1; i <= count2; i++){
      for(let start = 1; start <= 8; start++){
         const num = helper(i, start);
         if(num >= low && num <= high){
            res.push(num);
         };
      };
   };
   return res;
};

console.log(sequentialDigits(arr));

Output

[
   1234, 2345,
   3456, 4567,
   5678, 6789,
   12345
]

How It Works

The findCount function determines how many digits a number has. The helper function constructs sequential digit numbers starting from a given digit with a specified length. The main function iterates through all possible digit lengths and starting digits to generate valid sequential numbers within the range.

Key Points

  • Sequential digit numbers can only start from digits 1-8 (not 9, as there's no digit after 9)
  • The maximum length of a sequential digit number is 9 (123456789)
  • The algorithm generates numbers systematically by length and starting digit

Conclusion

This solution efficiently finds all sequential digit numbers within a given range by generating them systematically rather than checking every number in the range. The time complexity depends on the number of valid sequential digit numbers, not the size of the range.

Updated on: 2026-03-15T23:19:00+05:30

462 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements