1 / 6
Apr 2020

#include <stdio.h>
#include<string.h>
long int findrev(long int temp){
long int rem,revnum=0;
while(temp!=0){
rem=temp%10;
temp=temp/10;
revnum=revnum*10+rem;
}
return revnum;
}
void findpalindrome(long int num){
int count=0,rem;
long int revnum,temp;
while(count==0){
temp=num;
revnum=findrev(temp);
if(revnum==num)
count++;
else
num++;
}
printf("%ld",revnum);
}
int main() {
int size,count=0,revnum,rem,num,i;
scanf("%d",&size);
long int arr[100];
for( i=0;i<size;i++)
scanf("%ld",&arr[i]);
for( i=0;i<size;i++){
findpalindrome(arr[i]+1);
printf("\n");
}
return 0;
}

  • created

    Apr '20
  • last reply

    Apr '20
  • 5

    replies

  • 751

    views

  • 2

    users

  • 1

    link

Forget the TLE, you’ve got bigger problems - like many others before you, you’ve misread the constraints.

#include<stdio.h>
#include<string.h>
char increment(char ch){
if(ch==‘1’){
ch=‘2’;
return ch;
}
if(ch==‘2’){
ch=‘3’;
return ch;
}
if(ch==‘3’){
ch=‘4’;
return ch;
}
if(ch==‘4’){
ch=‘5’;
return ch;
}
if(ch==‘5’){
ch=‘6’;
return ch;
}
if(ch==‘6’){
ch=‘7’;
return ch;
}
if(ch==‘7’){
ch=‘8’;
return ch;
}
if(ch==‘8’){
ch=‘9’;
return ch;
}
if(ch==‘0’){
ch=‘1’;
return ch;
}

return ch;
}

int main()
{
long int size,i,firstplace=0;
long int lowerbound,upperbound;
scanf("%ld",&size);

char str[size][100000];

for( i=0;i<size;i++)
   scanf(" %s",str[i]);

for( i=0;i<size;i++){
   
    if(strlen(str[i])==1){
       
        if(str[i][0]!='9')
        str[i][0]=increment(str[i][0]);
        else{
        str[i][0]='1';
        str[i][1]='1';
        }
       
    }
   
    else
    {
        firstplace=0;
        while(str[i][firstplace]=='0')
        firstplace++;
      
     lowerbound=firstplace;
     upperbound=strlen(str[i])-1;
   
    while( ( lowerbound < upperbound ) && ( lowerbound < (( lowerbound + upperbound )+1)/2-1 ) ){
               str[i][upperbound]=str[i][lowerbound];
               upperbound--;
               lowerbound++;
        }
       
    if((( lowerbound + upperbound )+1)%2==0){
        if( str[i][lowerbound] < str[i][upperbound] ){
            str[i][lowerbound] = increment(str[i][lowerbound]);
            str[i][upperbound] = str[i][lowerbound];
        }
        else
        str[i][upperbound]=str[i][lowerbound];
    }
    else if((( lowerbound + upperbound )+1)%2!=0)
    {
        if(str[i][lowerbound+1]!='9')
        {
            if( str[i][lowerbound] < str[i][upperbound] ){
                str[i][upperbound] = str[i][lowerbound];
                str[i][lowerbound+1] = increment(str[i][lowerbound+1]);
            }
            else
            str[i][upperbound]=str[i][lowerbound];
        }
        else
        {
            if( str[i][lowerbound] < str[i][upperbound] ){
                str[i][lowerbound+1]='0';
                str[i][lowerbound]=increment(str[i][lowerbound]);
                str[i][upperbound]=str[i][lowerbound];
            }
            else
            str[i][upperbound]=str[i][lowerbound];
        }
    }
    }
   
    for(int j=firstplace;j<strlen(str[i]);j++)
    printf("%c",str[i][j]);
    printf("\n");
   
}

return 0;

}

resolved it but still, i did something wrong
i’am getting runtime error.can you guide me.
please

I replied to your other thread.

(btw, it’s considered bad form to post the same question in multiple threads. Post it once, then be patient.)