-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.java
More file actions
42 lines (41 loc) · 1.38 KB
/
Copy pathfile.java
File metadata and controls
42 lines (41 loc) · 1.38 KB
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
40
41
42
import java.util.Scanner;
public class file{
static int reverse(int num){
int temp = num,rev= 0;
for(;temp!=0;temp/=10)
rev = rev*10+temp%10;
return rev;
}
static boolean isPallendrome(int num){
if(num==reverse(num))
return true;
return false;
}
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-->0){
int n = input.nextInt(),temp=0;
for(int i=990;i>99;i-=11){
for(int j=999;j>99;j--){
int req = i*j;
if(req<n && isPallendrome(req)){
if(req>temp)
temp = req;
break;
}
}
}
System.out.println(temp);
}
}
}
/* Explanation:
suppose abccba be the pallendrome number(Remember input constrains are for six digit numbers)
a*100000+b*10000+c*1000+c*100+b*10+a
a*100001+b*10010+c*1100
11*(a*9091+b*910+c*100).Hence a pallendrome number must be 11 multiple
Question is largest pallendrome number i.e product of two 3-digit numbers
Hence either of the two numbers is a 11 multiple. Hence one loop from 990 to 99 with 11 units gap.
Other number from 999 to 99 with 1 gap.
*/