Finding the Nth term in a sequence formed by removing digit K from natural numbers | C ++ 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
#include <iostream> NEW_LINE using namespace std ; long long convertToBase9 ( long long n ) { long long ans = 0 ; long long a = 1 ; while ( n > 0 ) { ans += ( a * ( n % 9 ) ) ; a *= 10 ; n /= 9 ; } return ans ; } long long getNthnumber ( long long base9 , long long K ) { long long ans = 0 ; long long a = 1 ; while ( base9 > 0 ) { int cur = base9 % 10 ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 /= 10 ; a *= 10 ; } return ans ; } int main ( ) { long long N = 10 , K = 1 ; long long base9 = convertToBase9 ( N ) ; cout << getNthnumber ( base9 , K ) ; return 0 ; }