Largest Palindrome Product LeetCode Solution

Last updated on October 9th, 2024 at 05:53 pm

Here, We see Largest Palindrome Product LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

String

Companies

Yahoo

Level of Question

Hard

Largest Palindrome Product LeetCode Solution

Largest Palindrome Product LeetCode Solution

Problem Statement

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.

Example 1:
Input: n = 2
Output: 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Example 2:
Input: n = 1
Output: 9

1. Largest Palindrome Product Solution C++

class Solution {
public:
    int largestPalindrome(int n) {
        if(n==1)
        {
            return 9;
        }
        int hi=pow(10,n)-1;
        int lo=pow(10,n-1);
        int kk=1337;
        for(int i=hi;i>=lo;i--)
        {
            string s=to_string(i);
            string k=s;
            reverse(k.begin(),k.end());
            s+=k;
            long long int ll=stol(s);
            for(int j=hi;j>=sqrtl(ll);j--)
            {
                if(ll%j==0)
                {
                    return ll%kk;
                }
            }            
        }
        return 0;
    }
};

2. Largest Palindrome Product Solution Java

class Solution {
    public int largestPalindrome(int n) {
        if(n==1)
        return 9;
        if(n==2)
        return 987;
        if(n==3)
        return 123;
        if(n==4)return 597;
        if(n==5)return 677;
        if(n==6)return 1218;
        if(n==7)return 877;
        if(n==8)return 475;
        return 0;
    }
}

3. Largest Palindrome Product Solution JavaScript

var largestPalindrome = function(n) {
    if (n === 1) return 9;
    let hi = BigInt(Math.pow(10, n) - 1);
    let num = hi;
    while(num > 0) {
        num -= 1n;
        const palindrome = BigInt(String(num) + String(num).split('').reverse().join(''));
        for (let i = hi; i >= 2n; i -= 2n) {
            const j = palindrome / i; 
            if (j > hi) break;
            if (palindrome % i === 0n) {
                return String(palindrome % 1337n);
            };
        }
    }
};

4. Largest Palindrome Product Solution Python

class Solution(object):
    def largestPalindrome(self, n):
        if n == 1:
            return 9
        upper_bound = 10**n - 1
        lower_bound = 10**(n-1)

        for i in range(upper_bound, lower_bound, -1):
            palindrome = int(str(i) + str(i)[::-1])
            for j in range(upper_bound, int(palindrome**0.5), -1):
                if palindrome // j > upper_bound:
                    break
                if palindrome % j == 0:
                    return palindrome % 1337

solution = Solution()
print(solution.largestPalindrome(2))
print(solution.largestPalindrome(1))
Scroll to Top