Is my code efficient for determining the next positive integer palindrome?

Statement of problems: for a given positive number, I must find out immediately the next palindrome. For instance:

For 808, output:818
2133, output:2222

I want to know if my code is effective at all and how effective is it? Is this a good way to solve the problem?

Logical explanation: I set the inumbers to the extreme left, jto the right, and I basically compare 2 numbers. I always assign num[j]=num[i]and track if the number becomes greater than the original value or less than or equal. In the end, it is:, j-i==1 or j==idepending on the number of digits of an even or odd number, I see if the number has increased or not by making an appropriate decision.

EDIT: The number can be up to 100,000 digits! .. This was part of the problem statement, so I try to avoid brute force methods.

int LeftNineIndex = 0, RightNineIndex = 0;
bool NumberLesser = false, NumberGreater = false;
string number = Console.ReadLine();
char[] num = number.ToCharArray();
int i, j, x, y;
for (i = 0, j = num.Length - 1; i <= j; i++, j--)
{
      char m;
      Int32.TryParse(num[i].ToString(),out x);
      Int32.TryParse(num[j].ToString(), out y);
      if (x > y)
      {
           NumberGreater = true;
           NumberLesser = false;
      }
      else if (x < y)
      {
           if (j - i == 1)
           {
                NumberGreater = true;
                NumberLesser = false;
                x = x + 1;
                Char.TryParse(x.ToString(), out m);
                num[i] = m;
           }
           else
           {
                NumberGreater = false;
                NumberLesser = true;
           }             
     }

     if ((j == i && NumberGreater == false) || (j - i == 1 && x == y &&  NumberGreater == false))
     {
           if (x != 9)  // if the number is 9, then i can't add 1 to it
           {
                x = x + 1;
                Char.TryParse(x.ToString(), out m);
                num[i] = m;
           }
           else
           {
                if (num.Length != 1)
                {
                    Int32.TryParse(num[LeftNineIndex].ToString(), out x);
                    Int32.TryParse(num[RightNineIndex].ToString(), out y);
                    x = x + 1;
                    Char.TryParse(x.ToString(), out m);
                    num[LeftNineIndex] = m;
                    num[RightNineIndex] = m;
                }
                else
                {
                    // user has entered just '9', in which case I've hard-coded
                    Console.WriteLine("11");
                }
           }
     }
     num[j] = num[i];
     if (x != 9)  //gives us the index of the number closest to the middle, which is not 9
     {
          LeftNineIndex = i;
          RightNineIndex = j;
     }
}
Console.WriteLine(num);
+5
source share
2 answers

It is relatively easy to find the following palindrome in constant time:

  • Divide the entrance into two halves (the first half is larger if the length is odd)
  • Now for the next palindrome there are two candidates:
    • Save the first half and correct the second half.
    • The increment of the first half by 1 and the fixation of the second half
  • Select the first candidate if it is more than input, then select the second candidate.

A type BigIntegeris useful for implementing this:

This approach has a cost linear in the input length, i.e. logarithmic in number size.

public static BigInteger NextPalindrome(BigInteger input)
{
    string firstHalf=input.ToString().Substring(0,(input.ToString().Length+1)/2);
    string incrementedFirstHalf=(BigInteger.Parse(firstHalf)+1).ToString();
    var candidates=new List<string>();
    candidates.Add(firstHalf+new String(firstHalf.Reverse().ToArray()));
    candidates.Add(firstHalf+new String(firstHalf.Reverse().Skip(1).ToArray()));
    candidates.Add(incrementedFirstHalf+new String(incrementedFirstHalf.Reverse().ToArray()));
    candidates.Add(incrementedFirstHalf+new String(incrementedFirstHalf.Reverse().Skip(1).ToArray()));
    candidates.Add("1"+new String('0',input.ToString().Length-1)+"1");
    return candidates.Select(s=>BigInteger.Parse(s))
              .Where(i=>i>input)
              .OrderBy(i=>i)
              .First();
}

100000 .

. 9 s, , (9, 999,...).

+6

. , .

        private int FindNextPaladindrone(int value)
    {
        int result = 0;
        bool found = false;

        while (!found)
        {
            value++;
            found = IsPalindrone(value);
            if (found)
                result = value;
        }

        return result;
    }

    private bool IsPalindrone(int number)
    {
        string numberString = number.ToString();
        int backIndex;

        bool same = true;
        for (int i = 0; i < numberString.Length; i++)
        {
            backIndex = numberString.Length - (i + 1);
            if (i == backIndex || backIndex < i)
                break;
            else
            {
                if (numberString[i] != numberString[backIndex])
                {
                    same = false;
                    break;
                }
            }
        }
        return same;
    }
0

All Articles