I want to optimize the algorithm for converting a long long int (e.g=12345678) to a new long long int of this type (123456787654321) means the reverse of it has added. My current algorithm and function to do it is as;
long long ans_int(long long n){
long long temp=n;
n=n/10;
while(n>0){
long long reminder=n%10;
temp=temp*10+reminder;
n=n/10;
}
return temp;
}
I also tried the string approach but it's slower than the above approach. Can it be done using bit manipulation? Looking for a bit manipulation approach. I have very little knowledge about bit manipulation.
long long ans_int(long long n){
string r=to_string(n);
string t=r;
for(int i=r.length()-2;i>=0;i--){
t=t+r[i];
}
return stoi(t);
}
How can I do it in the fastest possible way?