title: Next Palindrome SPOJ (PALIN)
body: <p>While solving the NEXT PALINDROME question on SPOJ. I have considered for every possible case and wrote the code. But it is showing WA(Wrong Answer). I am not getting my mistake or any possible case where my code won't work.
  Below is my code.</p>
  
  <p>Please help me out.</p>
  
  <p>Thank You.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  char K[10000000];
  int a[10000000];
  int main()
  {
  int t,length,i,count,j,rem,carry;
  scanf("%d",&amp;t);
  while(t--)
  {
      scanf("%s",K);
      length=strlen(K);
      count=0;
      for(i=0;i&lt;length;i++)
      {
          a[i]=K[i]-'0';
          if(a[i]==9)
              count++;
      }
      if(length==2 &amp;&amp; count!=length)
      {
          if(a[0]&lt;=a[1])
          {
              a[1]=++a[0];
          }
          else
          {
              a[1]=a[0];
          }
          for(i=0;i&lt;length;i++)
          {
              printf("%d",a[i]);
          }
          printf("\n");
          continue;
      }
      if(count==length)
      {
          if(length==1)
          {
              printf("11\n");
              continue;
          }
          memset(a,0,sizeof(a));
          a[0]=10;
          a[length-1]=1;
          for(i=0;i&lt;length;i++)
          {
              printf("%d",a[i]);
          }
          printf("\n");
          continue;
      }
      i=0;
      j=length-1;
      count=0;
      while(j-i!=1 &amp;&amp; j-i!=0)
      {
          if(a[i]&gt;a[j])
          {
              a[j--]=a[i++];
              count++;
          }
          else
          {
              if(a[i]&lt;a[j])
                  count--;
              a[j--]=a[i++];
  
          }
  
      }
      if(count==0)
      {
          rem=a[i]+1;
          a[i]=rem%10;
          a[j++]=a[i--];
          carry=rem/10;
          while(carry!=0)
          {
              rem=a[i]+1;
              a[i]=rem%10;
              a[j++]=a[i--];
              carry=rem/10;
              if(i&lt;0&amp;&amp;j&gt;length-1)
                  break;
          }
      }
      else
      {
          if(a[i]&lt;a[j] || count&lt;0)
          {
              rem=a[i]+1;
              a[i]=rem%10;
              a[j++]=a[i--];
              carry=rem/10;
              while(carry!=0)
              {
                  rem=a[i]+1;
                  a[i]=rem%10;
                  a[j++]=a[i--];
                  carry=rem/10;
                  if(i&lt;0&amp;&amp;j&gt;length-1)
                      break;
              }
          }
          else
              a[j]=a[i];
      }
      for(i=0;i&lt;length;i++)
      {
          printf("%d",a[i]);
      }
      printf("\n");
  }
  return 0;
  </code></pre>
  
  <p>}</p>