Reputation: 1316
I've written a program to calculate largest palindrome product of 2 three digit number. I've solved this problem at Project Euler, but solving the same in HackerRank fails some test cases. I wonder what's wrong.
Input :
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Constraints:
1≤T≤100
101101<N<1000000
public class Solution {
static boolean isPalin ( int i){
int low = 0;
String a = String.valueOf(i);
int high = a.length() - 1;
while(low<high){
if(a.charAt(low) == a.charAt(high)){
low++;
high--;
}else{
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int noOfCases = in.nextInt();
int currMax = 0, result = 0;
int no_one, no_two;
int largest = -1;
for(int i=0; i<noOfCases; i++){
currMax = in.nextInt();
for(no_one = 100; no_one<=999; no_one++){
for(no_two = 101; no_two<=999; no_two++){
result = no_one * no_two;
if(isPalin(result)){
if(result > largest && result < currMax )
largest = result;
}
}
}
System.out.println(largest);
}
}
Upvotes: 0
Views: 136
Reputation: 33509
You are testing multiple cases, but only reset largest to -1 at the very start of the program.
Try adding largest=-1;
to the start of the loop over i.
Upvotes: 1