forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004. Largest palindrome product.cpp
58 lines (48 loc) · 1.12 KB
/
004. Largest palindrome product.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
bool ispalin(string s, int i ,int j)
{
if(i >= j)
return true;
if(s[i] != s[j])
return false;
return ispalin(s, i+1 , j-1);
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while(t--)
{
lli n;
cin>>n;
lli large= 0;
lli a,b,del;
for(a = 999; a > 99; a--)
{
if(a % 11 == 0)
{
b = 999;
del = 1;
}
else
{
b = 990;
del = 11;
}
while(b>=a)
{
string s = to_string(a*b);
if (a*b <= large)
break;
if(ispalin(s, 0, s.length()-1))
if(a*b < n)
large = max(a*b,large);
b = b-del;
}
}
cout<<large<<endl;
}
return 0;
}