class Solution {
public int largestPalindrome(int n) {
long upperBound = (long)Math.pow(10, n) - 1;
long lowerBound = (long)Math.pow(10, n - 1);
long maxNum = upperBound * upperBound;
long firstHalf = maxNum / (long)Math.pow(10, n);
while (firstHalf >= lowerBound) {
long palindrome = getPal(firstHalf);
for (long i = upperBound; i >= lowerBound; i--) {
if (palindrome / i > upperBound || i * i < palindrome) {
break;
}
if (palindrome % i == 0) {
return (int)(palindrome % 1337);
}
}
firstHalf--;
}
return (int)upperBound;
}
public long getPal(long i) {
StringBuilder sb = new StringBuilder();
return Long.parseLong(i + "" + sb.append(i).reverse().toString());
}
}

@ZhassanB I think the procedure you mentioned of finding a largest palindrome has a little bit problem. Say, the number is 1923, the left part is 19 which is less than the right part 23, but the next largest palindrome is not 1991 but 1881. So I think the procedure can be modified to "If the reverse of the left part is less than or equal to the right part, then the concatenation of left part and reversed left part is the next largest palindrome; Otherwise, decrease the left part."
But in this particular problem, it does not matter, since the multiple of 9..9 * 9..9 can always produce a number with both the left part and reversed left part are bigger than the right part.

@bluecloud As to the two numbers we are looking for, one of them must be x and another one should be u%x. so beyond sqrt(x), it is not possible to find any pairs of numbers that have the product of u.