Find all N digit integers with sequential digits
Last Updated :
22 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
vector< int > sequentialDigits( int n)
{
vector< int > arr;
long num = 0;
for ( int j = 0; j < n; j++)
num = num * 10 + (j + 1);
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;
}
int main()
{
int N = 4;
vector< int > ans = sequentialDigits(N);
for ( auto & it : ans)
cout << it << ' ' ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> sequentialDigits( int n)
{
Vector<Integer> arr = new Vector<Integer>();
long num = 0 ;
for ( int j = 0 ; j < n; j++)
num = num * 10 + (j + 1 );
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;
}
public static void main(String[] args)
{
int N = 4 ;
Vector<Integer> ans = sequentialDigits(N);
for ( int it : ans)
System.out.print(it + " " );
}
}
|
Python3
import math
def sequentialDigits(n):
arr = []
num = 0
for j in range ( 0 , n):
num = num * 10 + (j + 1 )
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
N = 4
ans = sequentialDigits(N)
for i in range ( 0 , len (ans)):
print (ans[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static List< int > sequentialDigits( int n)
{
List< int > arr = new List< int >();
long num = 0;
for ( int j = 0; j < n; j++)
num = num * 10 + (j + 1);
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;
}
public static void Main(String[] args)
{
int N = 4;
List< int > ans = sequentialDigits(N);
foreach ( int it in ans)
Console.Write(it + " " );
}
}
|
Javascript
<script>
const sequentialDigits = (n) => {
let arr = [];
let num = 0;
for (let j = 0; j < n; j++)
num = num * 10 + (j + 1);
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;
}
let N = 4;
let ans = sequentialDigits(N);
for (let it in ans)
document.write(`${ans[it]} `);
</script>
|
Output
1234 2345 3456 4567 5678 6789
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...