Posted on

Description

Submission

class Solution {
    typedef long long ll;
    const int M = 1337;
    ll createPalindrome(ll n) {
        string s = to_string(n);
        reverse_copy(s.begin(), s.end(), back_inserter(s));
        return stoll(s);
    }
public:
    int largestPalindrome(int n) {
        if(n == 1) return 9;
        ll lower = pow(10, n-1);
        ll upper = pow(10, n) - 1;

        for(int i = upper; i >= lower; --i) {
            ll p = createPalindrome(i);

            for(int j = upper; j >= sqrt(p); --j) {

                if(p % j == 0 && p / j >= lower) {
                    return p % M;
                }
            }
        }

        return -1;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *