Nth positive number whose absolute difference of adjacent digits is 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
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 ] + " " ) ; } let N = 21 ; findNthNumber ( N ) ;