0

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
def __init__
  • 1,092
  • 6
  • 17
  • 3
    What's the question you are trying to solve? These rearrange digits questions are usually very easy if you treat them as a string of digits and not an integer. – Richard Critten Jul 10 '21 at 20:04
  • 3
    @RichardCritten: Though that just hides the math in whatever functions you call to convert to and from a decimal string. But the system's functions for doing this are probably more efficient than yours. – Nate Eldredge Jul 10 '21 at 20:14
  • If you were doing it more than once you could, for example, create a lookup table for, say, 5 digits at a time. Then process the value in 5 digit chunks. – President James K. Polk Jul 10 '21 at 20:20
  • 1
    @NateEldredge But if you are reading in the inputs and read as std::string - no conversion necessary. These quiz questions usually start with read an integer - just to start you off on the wrong track. – Richard Critten Jul 10 '21 at 20:24
  • I don't think there is a *bit-manipulation* solution. As 10 is not a power of 2 you cannot really use bit-operations to manipulate the decimal representation of your number. – Socowi Jul 10 '21 at 20:25
  • Where do you get the input number from? – EOF Jul 10 '21 at 20:25
  • @EOF it's a subpart of the competitive programming CodeChef platform which involves this and exponential modular operation, I have already highly optimized the modular part, so only this can be optimized. ps: I was getting time limit errors. – def __init__ Jul 10 '21 at 20:29
  • @rajakr Which codechef problem? – yzt Jul 10 '21 at 20:37
  • You might be able to get a little faster (you have to measure) by using a modulo 100 and a lookup table (going two digits at a time, instead of one.) – yzt Jul 10 '21 at 20:38
  • The `to_string` function might be faster than doing divisions in a loop, but it's probably not a huge improvement. The rest of the code in the second snippet is definitely not an improvement. You can try combining the two snippets. Use `to_string` to get the array of digits, and use the multiply/add from the first snippet to compute the answer. – user3386109 Jul 10 '21 at 20:40
  • 2
    Time limit exceeded is often not about optimization at all, it's about whether you chose the exact algorithm the question expects or not. – Retired Ninja Jul 10 '21 at 20:47

0 Answers0