<p>Given an integer n, return <em>the <strong>largest palindromic integer</strong> that can be represented as the product of two <code>n</code>-digits integers</em>. Since the answer can be very large, return it <strong>modulo</strong> <code>1337</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 987
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 9
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 8</code></li>
</ul>

class Solution {
public:
    int largestPalindrome(int n) {
        if (n == 1) {
            return 9;
        }

        // Calculate the upper and lower bounds for n-digit numbers
        long long upperBound = pow(10, n) - 1;
        long long lowerBound = pow(10, n - 1);

        // Start checking from the largest possible product
        for (long long i = upperBound; i >= lowerBound; i--) {
            // Generate palindrome from the left half of i
            long long palindrome = generatePalindrome(i);

            // Check if the palindrome is a product of two n-digit numbers
            for (long long j = upperBound; j * j >= palindrome; j--) {
                if (palindrome % j == 0 && palindrome / j <= upperBound && palindrome / j >= lowerBound) {
                    return palindrome % 1337;
                }
            }
        }

        return -1; // Should never reach here
    }

private:
    // Helper function to generate palindrome from the left half of a number
    long long generatePalindrome(long long leftHalf) {
        string str = to_string(leftHalf);
        reverse(str.begin(), str.end());
        return stoll(to_string(leftHalf) + str);
    
    }
};
12500