I'm trying to submit this code on spoj https://www.spoj.com/problems/PALIN which is asking to find next smallest palindrome of a given number n, but as this code works, it is slow therefor it exceeds the time limit (2-9 seconds). is there another way to solve this exercise in a faster way?
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
code:
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t,k; string k2,k1;
cin>>t;
while(t--){
cin>>k;k++;
do{
k1=to_string(k);
k2=k1;
reverse(k1.begin(), k1.end());
if(k1==k2){cout<<k<<endl;}
k++;
}while(k1!=k2);
}
return 0;
}
example input:
2
808
2133
example output:
818
2222