Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digit integers. Since the answer can be very large, return it modulo 1337.
Function Signature
def largest_palindrome(n: int) -> int:
Rules
-
An
n
-digit integer is between
10^(n-1)
and
10^n - 1
inclusive. The two factors may be equal.
-
A palindromic integer reads the same forward and backward in decimal, for example
9009
.
-
Maximize the palindrome itself, then reduce it modulo
1337
.
Constraints
Examples
Input: n = 2
Output: 987
The largest palindrome product of two 2-digit numbers is 9009 = 91 * 99. Since 1337 * 6 = 8022, 9009 mod 1337 = 9009 - 8022 = 987.
Input: n = 1
Output: 9
Every two-digit palindrome is a multiple of 11, and 11 is a prime larger than any one-digit factor, so no two-digit palindrome qualifies. The largest palindrome product is 9 = 1 * 9 = 3 * 3, and 9 mod 1337 = 9.
Input: n = 3
Output: 123
The largest palindrome product of two 3-digit numbers is 906609 = 913 * 993. Since 1337 * 678 = 906486, 906609 mod 1337 = 123.