Nth positive number whose absolute difference of adjacent digits is at most 1
Last Updated :
16 Nov, 2021
Given a number N, the task is to find the Nth number which has an absolute difference of 1 between every pair of its adjacent digits.
Examples:
Input : N = 5
Output : 5
Explanation:
The first 5 such numbers are 1,2,3,4 and 5.
Input : N = 15
Output : 23
Explanation:
The first 15 such numbers are 1,2,3,4,5,6,7,8,9,10,11,12,21,22 and 23.
Approach: In order to solve this problem we are using the Queue data structure.
- Prepare an empty Queue, and Enqueue all integers 1 to 9 in increasing order.
- Now perform the following operation N times.
- Dequeue and store in array arr which stores ith number of required type in arr[i].
- If (arr[i] % 10 != 0), then enqueue 10 * arr[i] + (arr[i] % 10) – 1.
- Enqueue 10 * arr[i] + (arr[i] % 10).
- If (arr[i] % 10 != 9), then enqueue 10 * arr[i] + (arr[i] % 10) + 1.
- Return arr[N] as the answer.
Below is the implementation of the given approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNthNumber( int N)
{
long long arr[N + 1];
queue< long long > q;
for ( int i = 1; i <= 9; i++)
q.push(i);
for ( int i = 1; i <= N; i++) {
arr[i] = q.front();
q.pop();
if (arr[i] % 10 != 0)
q.push(arr[i] * 10 + arr[i] % 10 - 1);
q.push(arr[i] * 10 + arr[i] % 10);
if (arr[i] % 10 != 9)
q.push(arr[i] * 10 + arr[i] % 10 + 1);
}
cout<<arr[N]<<endl;
}
int main()
{
int N = 21;
findNthNumber(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findNthNumber( int N)
{
int []arr = new int [N + 1 ];
Queue<Integer> q = new LinkedList<>();
for ( int i = 1 ; i <= 9 ; i++)
q.add(i);
for ( int i = 1 ; i <= N; i++)
{
arr[i] = q.peek();
q.remove();
if (arr[i] % 10 != 0 )
q.add(arr[i] * 10 + arr[i] % 10 - 1 );
q.add(arr[i] * 10 + arr[i] % 10 );
if (arr[i] % 10 != 9 )
q.add(arr[i] * 10 + arr[i] % 10 + 1 );
}
System.out.println(arr[N]);
}
public static void main(String[] args)
{
int N = 21 ;
findNthNumber(N);
}
}
|
Python3
def findNthNumber(N):
arr = [ 0 for i in range (N + 1 )]
q = []
for i in range ( 1 , 10 , 1 ):
q.append(i)
for i in range ( 1 , N + 1 , 1 ):
arr[i] = q[ 0 ]
q.remove(q[ 0 ])
if (arr[i] % 10 ! = 0 ):
q.append(arr[i] * 10 + arr[i] % 10 - 1 )
q.append(arr[i] * 10 + arr[i] % 10 )
if (arr[i] % 10 ! = 9 ):
q.append(arr[i] * 10 + arr[i] % 10 + 1 )
print (arr[N])
if __name__ = = '__main__' :
N = 21
findNthNumber(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findNthNumber( int N)
{
int []arr = new int [N + 1];
Queue< int > q = new Queue< int >();
for ( int i = 1; i <= 9; i++)
q.Enqueue(i);
for ( int i = 1; i <= N; i++)
{
arr[i] = q.Peek();
q.Dequeue();
if (arr[i] % 10 != 0)
q.Enqueue(arr[i] * 10 +
arr[i] % 10 - 1);
q.Enqueue(arr[i] * 10 + arr[i] % 10);
if (arr[i] % 10 != 9)
q.Enqueue(arr[i] * 10 +
arr[i] % 10 + 1);
}
Console.WriteLine(arr[N]);
}
public static void Main(String[] args)
{
int N = 21;
findNthNumber(N);
}
}
|
Javascript
<script>
function findNthNumber(N)
{
let arr = new Array(N + 1);
let q = [];
for (let i = 1; i <= 9; i++)
q.push(i);
for (let i = 1; i <= N; i++)
{
arr[i] = q[0];
q.shift();
if (arr[i] % 10 != 0)
q.push(arr[i] * 10 + arr[i] % 10 - 1);
q.push(arr[i] * 10 + arr[i] % 10);
if (arr[i] % 10 != 9)
q.push(arr[i] * 10 + arr[i] % 10 + 1);
}
document.write(arr[N] + "</br>" );
}
let N = 21;
findNthNumber(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...