[Geeks for Geeks] Nth Natural Number

Nth Natural Number

Given a positive integer N. You have to find Nth natural number after removing all the numbers containing digit 9.

  • Time : O(log9N)
  • Space : O(log9N)
1
2
3
4
5
6
7
8
9
10
11
12
class Solution{
public:
long long findNth(long long N){
string s = "";
while(N) {
s.push_back(N % 9 | 0b110000);
N /= 9;
}
reverse(begin(s), end(s));
return stol(s);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/17/PS/GeeksforGeeks/nth-natural-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.