Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Largest Palindrome Product problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Largest Palindrome Product problem solution, we have given an integer n, return the largest palindromic integer that can be represented as the product of two n-digits integers. Since the answer can be very large, return it modulo 1337.

Leetcode Largest Palindrome Product problem solution

Leetcode Largest Palindrome Product problem solution in Python.

class Solution:
    def largestPalindrome(self, n: int) -> int:
        if n == 1:
            return 9
        min_num = 10 ** (n - 1)
        max_num = 10 ** n - 1       
        
        max_pal = 0
        for i in range(max_num, min_num - 1, -2): 
            if i * i < max_pal:
                break
                
            for j in range(max_num, i - 1, -2):
                product = i * j
                if product % 11 != 0 and product >= max_pal:
                    continue
                if str(product) == str(product)[::-1]:
                    max_pal = product

        return max_pal % 1337

Largest Palindrome Product problem solution in Java.

public int largestPalindrome(int n) {
        if (n == 1) return 9;
        int max = (int) Math.pow(10, n) - 1;
        int max_11 = (max / 11)  * 11;
        long product;
        for (int i=max; i > (int) Math.pow(10, n-1); i--){
            product = Long.parseLong(i + new StringBuilder(String.valueOf(i)).reverse().toString());
            for (long j = max_11; j > (int) Math.pow(10, n-1); j-=11) {
                if ((product/j) / (max+1) == 0){ 
                    if (product%j==0) return (int)(product% 1337);
                }
                else break;
            }
        }
        return -1;
    }

Problem solution in C++.

class Solution {
public:
    int largestPalindrome(int n) {
        long max, min, ans = 0, sum;
        max = static_cast<long>(pow(10, n)) - 1;
        min = max/10 + 1;
        sum = 2 * max;
        while (sum/2 * (sum - sum/2) > ans){
            long i = sum/2, j = sum - sum/2;
            while (j <= max && i >= min){
                long num = i * j;
                if (num > ans && isPalindrome(num)){
                    ans = num;
                    break;
                }
                i--;
                j++;
            }
            sum--;
        }
        return ans % 1337;
    }
    bool isPalindrome(long x){
        long y = 0;
        for (long z = x; z != 0; y = 10 * y + z % 10, z /= 10);
        return x == y;
    }
};

Problem solution in C.

long int creatPalindrome(long int num,int n){
    long int p=num*pow(10,n);
    for(int i=0;i<n;i++){
        p=p+(num/(long int)pow(10,n-i-1))*(long int)pow(10,i);
        num=num%(long int)pow(10,n-i-1);
    }
    return p;
}
int largestPalindrome(int n) {
    if(n==1){return 9;}
    long int p=pow(10,n)-1;
    long int q=p;
    long int temp=pow(10,n-1);
    long int ret=0;
    long int ret1=0;
    while(p>=temp){ 
        ret=creatPalindrome(p,n);
        for(int i=q;i>=temp;i--){
            ret1=ret/i;
            if(ret1>=i){break;}
            if(ret1>=temp&&ret%i==0){return ret%1337;}
        }
        p--;
    }
    return NULL;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes