Skip to content

479. Largest Palindrome Product #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
namespace-io opened this issue Oct 10, 2019 · 0 comments
Open

479. Largest Palindrome Product #347

namespace-io opened this issue Oct 10, 2019 · 0 comments

Comments

@namespace-io
Copy link
Owner

class Solution {
public:
    int largestPalindrome(int n) {
        if(n == 1) return 9;
        int upper = pow(10, n) - 1;
        int lower = pow(10, n-1);
        for(int d = upper; d >= lower; d--){
            long p = buildPa(d);
            for(long m = upper; m * m >= p; m--){
                if(p % m == 0) return p % 1337;
            }
        }
        
        return 0;
    }
    
    long buildPa(int d){
        string s = to_string(d);
        reverse(s.begin(), s.end());
        return stol(to_string(d) + s);
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant