The Next Palindrome

QUESTION

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K, write the value of the smallest palindrome larger than K to output.\n\nInput\nThe first line contains an integer, which corresponds to K. Assume that K is less than200000.\nOutput\nOutput consists of a single integer, which corresponds to the smallest palindrome larger than K.\n\n.

“TESTCASE_1”: “808\n###—###SEPERATOR—###—\n818”, “TESTCASE_2”: “2133\n###—###SEPERATOR—###—\n2222”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
        #include<string.h>
        #include<stdlib.h>
        int main()
        {
            char a[1000002];
            int i,flag,check,l,m,k;
           
            
                flag=1;
                  scanf("%[^\n]%*c", a);

                l=strlen(a);
                //printf
                k=l>>1;
                check=0;
                for(i=0;i<l;i++)
                {
                    if(a[i]!='9')
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    a[0]='1';
                    for(i=1;i<l;i++)
                    a[i]='0';
                    a[l]='1';
                    a[l+1]='\0';
                    check=1;
                }
                flag=0;
                if(check!=1)
                {
                    for(i=0;i<k;i++)
                    {
                        if(a[i]<a[l-1-i])
                        flag=-1;
                        else if(a[i]>a[l-1-i])
                        flag=1;
                        a[l-1-i]=a[i];
                    }
         
                    if(!(l&1))
                    m=k-1;
                    else m=k;
                    if(flag==0||flag==-1)
                    {
                        i=0;
                        while(a[m-i]=='9')
                        {
                            a[m-i]='0';
                            a[l-1-m+i]='0';
                            i++;
                        }
         
                        a[m-i]++;
                        a[l-1-m+i]=a[m-i];
                }
            
            printf("%s\n",a);
            }
            return 0;
        }  
 
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO