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 7822297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:53:18+00:00 2026-06-02T07:53:18+00:00

I came across this problem from CodeChef . The problem states the following: A

  • 0

I came across this problem from CodeChef. The problem states the following:

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 of not more than 1000000
digits, write the value of the smallest palindrome larger than K to
output.

I can define a isPalindrome method as follows:

def isPalindrome(someNumber:String):Boolean = someNumber.reverse.mkString == someNumber

The problem that I am facing is how do I loop from the initial given number and break and return the first palindrome when the integer satisfies the isPalindrome method? Also, is there a better(efficient) way to write the isPalindrome method?

It will be great to get some guidance here

  • 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-02T07:53:20+00:00Added an answer on June 2, 2026 at 7:53 am

    If you have a number like 123xxx you know, that either xxx has to be below 321 – then the next palindrom is 123321.

    Or xxx is above, then the 3 can’t be kept, and 124421 has to be the next one.

    Here is some code without guarantees, not very elegant, but the case of (multiple) Nines in the middle is a bit hairy (19992):

    object Palindrome extends App {
    
    def nextPalindrome (inNumber: String): String = {
      val len = inNumber.length ()
      if (len == 1 && inNumber (0) != '9') 
        "" + (inNumber.toInt + 1) else {
        val head = inNumber.substring (0, len/2)
        val tail = inNumber.reverse.substring (0, len/2)
        val h = if (head.length > 0) BigInt (head) else BigInt (0)
        val t = if (tail.length > 0) BigInt (tail) else BigInt (0)
    
        if (t < h) {
          if (len % 2 == 0) head + (head.reverse)
          else inNumber.substring (0, len/2 + 1) + (head.reverse)
        } else {
         if (len % 2 == 1) {
           val s2 = inNumber.substring (0, len/2 + 1) // 4=> 4
           val h2 = BigInt (s2) + 1  // 5 
           nextPalindrome (h2 + (List.fill (len/2) ('0').mkString)) // 5 + "" 
         } else {
           val h = BigInt (head) + 1
           h.toString + (h.toString.reverse)
         }
        }
      }
    }
    
    def check (in: String, expected: String) = {
      if (nextPalindrome (in) == expected) 
        println ("ok: " + in) else 
        println (" - fail: " + nextPalindrome (in) + " != " + expected + " for: " + in)
    }
    //
    val nums = List (("12345", "12421"), // f
      ("123456", "124421"), 
      ("54321", "54345"), 
      ("654321", "654456"), 
      ("19992", "20002"),
      ("29991", "29992"),
      ("999", "1001"),
      ("31", "33"),
      ("13", "22"),
      ("9", "11"),
      ("99", "101"),
      ("131", "141"),
      ("3", "4")
    )
    nums.foreach (n => check (n._1, n._2))
    println (nextPalindrome ("123456678901234564579898989891254392051039410809512345667890123456457989898989125439205103941080951234566789012345645798989898912543920510394108095"))
    
    }
    

    I guess it will handle the case of a one-million-digit-Int too.

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

Sidebar

Related Questions

I came across this following arithmetic problem. But the result is different from normal
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
I came across this problem while preparing for an interview and curious to know
I came across this problem when trying to decode json into an array ,
i came across this problem, my code in the viewWillAppear method is not executed
I am new to creating Java web applications and came across this problem when
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I was configuring syntax highlighting for my blog when I came across this problem.
I am watching a video about [LINQ][1] and came across a problem. In this
I just came across this weird problem that is happening in IE. In the

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.