C++ code getting TLE at n = 7


  • 0
    R

    That code works at Geeksforgeeks website, which has the same question except the mod1337 part. But I get TLE when n= 7. n=8 works fine.

    int largestPalindrome(int n) {
            long long maxProd = 0;
            long long low = pow(10, n-1);
            long long high = low*10-1;
            
            for(long long i = high; i >= low; i--) {
                for(long long j = i; j >= low; j--) {
                    long long prod = i*j;
                    if(prod <= maxProd) break;
                    long long rev = 0, num = prod;
                    while(num != 0) {
                        rev = rev * 10 + num % 10;
                        num/=10;
                    }
                    if(prod == rev && prod > maxProd) maxProd = prod;
                }
            }
            return maxProd%1337;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.