Largest Palindrome product Time limit exceeded solution


  • 0
    H
        public int largestPalindrome(int n) {
            
            /////////// creating the biggest number within the range ////////////////
            
            int s=0;
            for(int i=1;i<=n;i++){
                s=s*10+9;
            }
            
            /////////// creating the smallest number within the given range /////////////
            int l=1;
            for(int i=2;i<=n;i++){
                l=l*10;
            }
                    
            ////////// Multiplying the numbers and finding the largest palindrome //////////////////////////////////////////
            
            int product=0, rev=0, finaln=0, max=0;        
            for(int i=s;i>=l;i--) {
                for(int j=s;j>=l;j--){
                    product= i*j;
                    finaln=product;
                    rev = 0;
                    while(product>=1){
                        rev=rev*10+(product%10);
                        product=product/10;
                    }
                    if(rev==finaln){
                        if(finaln > max)
                            max = finaln;
                    }
                }
            }
            return max%1337;
        }
    }
    

Log in to reply
 

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