Project Euler #4: Largest palindrome product

Question

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

Answer : 906609

Hacker Rank Question
Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;

public class Solution {

    public static int check(String s){
        int len=s.length();
        for(int i=0;i<len/2;i++){
            if(s.charAt(i)!=s.charAt(len-1-i))return 0;
        }
        return 1;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a[]=new int[1000001];
        int k=0,count=0;

        for(int i=100;i<=999;i++){
            for(int j=100;j<=999;j++){
                int pro=i*j;
                if(pro>=101101){
                    int val=check(""+pro);
                    if(val==1)a[pro]=1;
                }
                else a[pro]=0;
            }
        }
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            int n = in.nextInt();
            for(int i=n-1;i>=101101;i--){

                if(a[i]==1){
                    System.out.println(i);
                    break;
                }
            }
        }
    }
}

Comments

Popular Posts