# 3 ms C++ Recursive Solution 3 line function body

• Binary numbers (Base 2) are from 0 to 1 and next numbers are 10,11,100.........

octal numbers (Base 8) are from 0 to 7 and next numbers are 10,11,12.......17,20,............

when we remove all 9's the numbers are from 0 to 8 and next numbers are 10,11,12....18,20,.....

hence after removing 9 we get Base 9. so finding this Base 9 number is similar to Binary Number (Base 2) & octal numbers (Base 8).

we need to use Division and modulo of 9 instead of 2 (for Binary) or 8 (for octal).

Example:
if n = 10;
10%9 * 10^0 + (10/9)%9*10^1;
1+10 = 11;

``````class Solution {
public:
int sum = 0, i = 0 ;
int newInteger(int n) {
if(n == 0) return sum;
sum += n%9*pow(10,i++);
return newInteger(n/9);
}
};
``````

• class Solution {
public:
int sum = 0, i = 0 ;
int newInteger(int n) {
if(n == 0) return sum;
sum += n%9*pow(10,i++);
return newInteger(n/9);
}
};

Please use the formatting capability built into the editor, otherwise, your post is very hard to read. (Use 3 backticks before and after block of code)

Like so:

``````class Solution {
public:
int sum = 0, i = 0 ;
int newInteger(int n) {
if(n == 0) return sum;
sum += n%9*pow(10,i++);
return newInteger(n/9);
}
};
``````

• @Emmojo Thank you

• You need not to have recursion here. Simple while loop with do.

``````while (n > 0) {
// code
}
``````

Function calls are expensive to make and use lots of system stack, causing slower performance.

• @vishal51 Thanks for your suggestion.
But I made this post just to show that it can be solved using recursion.