Nth positive number whose absolute difference of adjacent digits is at most 1 | C ++ Program to find Nth number with absolute difference between all adjacent digits at most 1. ; Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }