Largest Palindrome Product Problem
Description
LeetCode Problem 479.
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
Solution
Approach: Try Palindromes from the Largest Down
There are far fewer palindromes than products, so instead of multiplying pairs of numbers, generate the candidate palindromes in decreasing order and stop at the first one that splits into two n-digit factors.
Let upper = 10n − 1 and lower = 10n−1, the largest and smallest n-digit numbers. For n >= 2:
- Go through
halffromupperdown tolower. Mirroringhalfgives the 2n-digit palindromehalf + reverse(half)(forhalf = 99, it is9999). These palindromes come out in decreasing order. - For each palindrome, look for a factor
factor <= uppersuch thatpalindrome / factoralso has at mostndigits, i.e.factor * upper >= palindrome. The first palindrome that has one is the largest, so return it% 1337.
The search for a factor is where the time goes, and one fact cuts it by a factor of 11: every palindrome with an even number of digits is divisible by 11. (Its digits pair up as positions i and 2n - 1 - i, one odd and one even, so the alternating digit sum is 0.) Since 11 is prime, one of the two factors must be a multiple of 11. So factor only has to run over the multiples of 11, from the largest one <= upper down, and checking factor * upper >= palindrome (rather than factor * factor >= palindrome) covers the case where the multiple of 11 is the smaller factor.
n = 1 is a special case: every 2-digit palindrome is a multiple of 11, which no product of two 1-digit numbers is, so the answer is the 1-digit 9.
The palindromes reach 16 digits, so use 64-bit integers (BigInt in JavaScript).
Complexity: O(102n) time in the worst case (up to 10n values of half, each with up to 10n/11 factors), but the search stops early: for n = 8 it ends after about 104 values of half with at most about 103 factors each. O(1) space.
class Solution:
def largestPalindrome(self, n: int) -> int:
if n == 1:
return 9 # no 2-digit palindrome is a product of two 1-digit numbers
upper = 10 ** n - 1 # largest n-digit number
lower = 10 ** (n - 1) # smallest n-digit number
# A palindrome with an even number of digits is a multiple of 11, so one factor is too.
largest_multiple_of_11 = upper - upper % 11
# Try 2n-digit palindromes from the largest down: the first one that factors is the answer.
for half in range(upper, lower - 1, -1):
palindrome = self.mirror(half)
factor = largest_multiple_of_11
# The other factor, palindrome / factor, must have at most n digits.
while factor * upper >= palindrome:
if palindrome % factor == 0:
return palindrome % 1337
factor -= 11
return -1 # not reached for 1 <= n <= 8
def mirror(self, half: int) -> int:
"""half followed by its digits reversed, e.g. 123 -> 123321."""
result = half
rest = half
while rest > 0:
result = result * 10 + rest % 10
rest //= 10
return result
Now write it from memory
Close the write-up and solve it yourself against the tests.