https://leetcode.com/problems/largest-palindrome-product/description/
long getCandidate(int n) {
string s = to_string(n);
reverse(s.begin(), s.end());
return stol(to_string(n) + s);
}
int largestPalindrome(int n) {
if(n==1) return 9;
int mx = pow(10,n) - 1;
int mi = pow(10,n-1);
for(int i=mx; i>=mi; i--) {
long c = getCandidate(i);
for(long j=mx; j*j>=c; j--) {
if(c%j == 0 && c/j >= mi) {
return c%1337;
}
}
}
return -1;
}
No comments:
Post a Comment