Algorithm
Problem Name: 479. Largest Palindrome Product
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
Constraints:
1 <= n <= 8
Code Examples
#1 Code Example with Javascript Programming
Code -
Javascript Programming
function largestPalindrome(n) {
if(n === 1) return 9
let max = BigInt(10 ** n - 1), min = max / 10n + 1n
for(let h = max; h >= min; h--) {
let left = h, right = 0n
for(let i = h; i !== 0n; ) {
right = right * 10n + i % 10n
i = i / 10n
left *= 10n
}
let pal = left + right
for(let i = max; i >= min; i--) {
let j = pal / i
if(j > i) break
if(pal % i === 0n) return pal % 1337n
}
}
}
Copy The Code &
Try With Live Editor
Input
n = 2
Output
987
#2 Code Example with Python Programming
Code -
Python Programming
class Solution:
def largestPalindrome(self, n):
"""
:type n: int
:rtype: int
"""
return list(num for i,num in enumerate([0,9,987,123,597,677,1218,877,475]) if i==n)[0]
Copy The Code &
Try With Live Editor
Input
n = 2
Output
987