Finding the Nth term in a sequence formed by removing digit K from natural numbers | Javascript implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
function convertToBase9 ( n ) { let ans = 0 ; let a = 1 ; while ( n > 0 ) { ans += a * ( n % 9 ) ; a *= 10 ; n = Math . floor ( n / 9 ) ; } return ans ; } function getNthnumber ( base9 , K ) { let ans = 0 ; let a = 1 ; while ( base9 > 0 ) { let cur = base9 % 10 ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 = Math . floor ( base9 / 10 ) ; a *= 10 ; } return ans ; } let N = 10 , K = 1 ; let base9 = convertToBase9 ( N ) ; document . write ( getNthnumber ( base9 , K ) ) ;