We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
System.out.println(largestPalindrome(n));
}
}
public static long palindrome(long a){
long rev = 0;
while(a!=0){
rev = rev*10 + a%10;
a = a/10;
}
return rev;
}
public static boolean check(long a){
return a == palindrome(a);
}
public static long largestPalindrome(long n){
long largest = 0;
long res=0;
for(int i=999; i>=100; i--){
for(int j=999; j>=100; j--){
res = j*i;
if(check(res) && res<n){
largest = Math.max(largest,res);
}
}
}
return largest;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #4: Largest palindrome product
You are viewing a single comment's thread. Return to all comments →
Java Solution;
import java.util.; import java.text.; import java.math.; import java.util.regex.;
public class Solution {
}