Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:09:54+00:00 2026-06-03T23:09:54+00:00

Problem statement: For a given positive number, I have to find out the immediately

  • 0

Problem statement: For a given positive number, I have to find out the immediately next palindrome. Eg:

For 808, output:818
2133, output:2222

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

Logic explanation: I’ve set i to the leftmost position of the number, j to the rightmost position an I’m basically comparing the 2 numbers. I assign num[j]=num[i] always, and keep track if the number becomes greater than the original value, or lesser, or equal. In the end,that is: j-i==1 or j==i, depending on number of digits of the number being even or odd, I see if the number became greater or not, taking a decision accordingly.

EDIT: The number can be upto 100,000 digits long!..That was part of the problem statement,so I’m trying 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:09:56+00:00Added an answer on June 3, 2026 at 11:09 pm

    It’s relatively simple to find the next palindrome in constant time:

    • Split the input in two halves (first half larger if length is odd)
    • Now for the next palindrome there are two candidates:
      1. Keep the first half, and fix the second half
      2. Increment the first half by 1, and fix the second half
    • Choose the first candidate if it is larger than the input, choose the second candidate otherwise.

    The BigInteger type is useful for implementing this:

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

    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();
    }
    

    Tested to work with all positive integers below 100000 by comparing with the native implementation.

    The fifth case is easy to miss. If the number consists of all 9s, incrementing the first half changes the length, and requires extra handling if the current length is odd (9, 999,…).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below Problem Statement PS: Given a string str and a Non-Empty
I have a stored Procedure (Generate_Insert)which will output an Insert statement as output given
Problem Statement is somewhat like this: Given a website, we have to classify it
I have a problem with a continue statement in my C# Foreach loop. I
I have a problem with the SQL statement detailed below. The query returns the
I have an big problem with an SQL Statement in Oracle. I want to
I have a problem creating the following SQL Statement using LINQ & C# select
I have a problem with a sql-statement. My DB is MySQL 5.1. I have
This is a spin-off of one of my earlier questions Problem statement: Given a
UPDATE:: OK i am putting the original problem statement here Given the Main class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.