What's wrong with my code?(NXTPALIN)

I brute forced it to increment the input untill it gives a palindrome.
################################################
#include<bits/stdc++.h>
using namespace std;

bool nxtpalin(int n){
unsigned int rem = 0 , total = 0, lol = n;
while (n > 0) {
rem = n % 10;
total = total * 10 + rem;
n /= 10;
}
if (lol == total) {
return true;
}
else{
return false;
}
}

int main(){

int t;
cin>>t;

while (t--) {
	unsigned int n;
	cin>>n;
	unsigned listi[1000];

	for (int i = 0; i < 1000; i++) {
		listi[i]  = n + i + 1;
	}
	for (int i = 0; i < 1000; i++) {
		if (nxtpalin  (listi [i])) {
			cout<<listi[i]<<"\n";
			break;
		}
	}
}

}

What you’re doing will give you a tle because you’re calculating the palindrome of every number for every test case so basically you’re doing extra work, store the palindrome numbers beforehand and then use can do binary search or something.
If you still don’t understand , refer to this

1 Like