Largest Palindrome Product - Problem

Given an integer n, return the largest palindromic integer that can be represented as the product of two n-digit integers.

Since the result can be very large, return it modulo 1337.

For example, if n = 2, we need to find the largest palindrome that is the product of two 2-digit numbers (from 10 to 99). The largest palindrome would be 9009 = 91 × 99.

Note: For n = 1, the answer is 9 since 3 × 3 = 9 is the largest palindromic product of two 1-digit numbers.

Input & Output

Example 1 — Basic Case
$ Input: n = 2
Output: 906
💡 Note: We need the largest palindrome from products of 2-digit numbers (10-99). The largest is 9009 = 91 × 99. Return 9009 % 1337 = 906.
Example 2 — Single Digit
$ Input: n = 1
Output: 9
💡 Note: For 1-digit numbers (1-9), the largest palindromic product is 3 × 3 = 9. Since 9 < 1337, we return 9.
Example 3 — Larger Case
$ Input: n = 3
Output: 123
💡 Note: For 3-digit numbers (100-999), we find the largest palindromic product and return it modulo 1337. The actual palindrome is much larger than 1337.

Constraints

  • 1 ≤ n ≤ 8

Visualization

Tap to expand
Largest Palindrome Product Palindrome Generation Approach INPUT n = 2 Number of digits 2-digit number range: 10 to 99 Max possible palindrome: 99 x 99 = 9801 Palindrome range: 1001 to 9999 (4-digit palindromes) ALGORITHM STEPS 1 Generate Palindrome Start from largest: 9999 Then 9889, 9779... 2 Check Factorization Can palindrome be product of two n-digit numbers? 3 Find First Valid 9009 = 91 x 99 OK - Valid! 4 Apply Modulo 9009 mod 1337 = 906 9 0 0 9 Reads same both ways FINAL RESULT 906 Calculation: Largest palindrome: 9009 9009 % 1337 = 906 (91 x 99 = 9009) Output Verified Answer: 906 Output: 906 Key Insight: Generate palindromes from largest to smallest by mirroring the first half. For 2n-digit palindromes, iterate first half from 10^n-1 down. First palindrome factorable into two n-digit numbers is the answer. This is faster than checking all products because palindromes are rare among large numbers. TutorialsPoint - Largest Palindrome Product | Palindrome Generation Approach
Asked in
Google 12 Facebook 8 Microsoft 6
12.5K Views
Medium Frequency
~35 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen