Open In App

Find all N digit integers with sequential digits

Last Updated : 22 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find all N digit integers with sequential digits

Examples:

Input: N = 4
Output: {1234, 2345, 3456, 4567, 5678, 6789}
Explanation: All 4 digit integers with sequential digits are: {1234, 2345, 3456, 4567, 5678, 6789}

Input: N = 6
Output: {123456, 234567, 345678, 456789} 

 

Approach: The task can be solved by finding the first N digit number with sequential digits and then adding (111 … N times) to it, till the last number is less than equal to (999 … N times). Follow the below steps to solve the problem:

  • For any N, the 1st sequential digit number will be (123 … N times)
  • So store the first sequential digit number of length N in num
  • Since every sequential digit number will differ by (111 … N times), store this value in another variable add.
  • Now with the help of loop, find all sequential digit numbers of length N, by just adding add to num in each iteration

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find all the N digit
// integers with sequential digits
vector<int> sequentialDigits(int n)
{
    vector<int> arr;
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
        num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
        add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && floor(log10(num) + 1) == n) {
 
        arr.push_back(num);
        num += add;
    }
 
    return arr;
}
 
// Driver Code
int main()
{
    int N = 4;
    vector<int> ans = sequentialDigits(N);
 
    // Print the required numbers
    for (auto& it : ans)
        cout << it << ' ';
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find all the N digit
  // integers with sequential digits
  static Vector<Integer> sequentialDigits(int n)
  {
    Vector<Integer> arr = new Vector<Integer>();
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
      num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
      add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && Math.floor(Math.log10(num) + 1) == n) {
 
      arr.add((int) num);
      num += add;
    }
 
    return arr;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4;
    Vector<Integer> ans = sequentialDigits(N);
 
    // Print the required numbers
    for (int it : ans)
      System.out.print(it +" ");
  }
}
 
// This code is contributed by shikhasingrajput


Python3




# Python program for the above approach
import math
 
# Function to find all the N digit
# integers with sequential digits
def sequentialDigits(n):
 
    arr = []
 
    # Stores the first N-digit
    # sequential number
    # (123 ... N times)
    num = 0
    for j in range(0, n):
        num = num * 10 + (j + 1)
 
    # Stores the difference
    # (111 ... N times)
    add = 0
    for i in range(0, n):
        add = add * 10 + 1
 
    while (num % 10 > 0 and num % 10 <= 9 and math.floor(math.log10(num) + 1) == n):
 
        arr.append(num)
        num += add
 
    return arr
 
# Driver Code
N = 4
ans = sequentialDigits(N)
 
# Print the required numbers
for i in range(0, len(ans)):
    print(ans[i], end=" ")
 
# This code is contributed by Taranpreet


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find all the N digit
  // integers with sequential digits
  static List<int> sequentialDigits(int n)
  {
    List<int> arr = new List<int>();
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
      num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
      add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && Math.Floor(Math.Log10(num) + 1) == n) {
 
      arr.Add((int) num);
      num += add;
    }
 
    return arr;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 4;
    List<int> ans = sequentialDigits(N);
 
    // Print the required numbers
    foreach (int it in ans)
      Console.Write(it +" ");
  }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
    // JavaScript program for the above approach
 
    // Function to find all the N digit
    // integers with sequential digits
    const sequentialDigits = (n) => {
        let arr = [];
 
        // Stores the first N-digit
        // sequential number
        // (123 ... N times)
        let num = 0;
        for (let j = 0; j < n; j++)
            num = num * 10 + (j + 1);
 
        // Stores the difference
        // (111 ... N times)
        let add = 0;
        for (let i = 0; i < n; i++)
            add = add * 10 + 1;
 
        while (num % 10 > 0 && num % 10 <= 9
            && Math.floor(Math.log10(num) + 1) == n) {
 
            arr.push(num);
            num += add;
        }
 
        return arr;
    }
 
    // Driver Code
    let N = 4;
    let ans = sequentialDigits(N);
 
    // Print the required numbers
    for (let it in ans)
        document.write(`${ans[it]} `);
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

1234 2345 3456 4567 5678 6789 

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads