amojobim
BAN USERI solved this problem a while ago, it was one of the classical problem from the Sphere Online Judge website. I used a list of integer to represent the digits of the number. The first thing you can do is "palindromize" the array, i.e. make the lower digits equal to the higher ones (146236 -> 146641). This works if the result is a bigger number. You should check for this condition beforehand. If the condition is not true, you need to increment the digit(s) in the middle and, if necessary, carry over the 1 to the previous digit(s) (196 -> 202 or 1996 -> 2002).
I don't have the code right now. It needs a bit of thought but it's reasonably easy and can handle even huge numbers since you will not actually compare numbers but just the digits. For example, if you have 136, compare 1 and 6 and you know that "palindromizing" the number won't work.
I hope that helps.
Yeah, the code can be very tricky especially if you want it to work for huge number (which was a requirement for the SPOJ problem). I hope the algorithm as I described it makes sense.
- amojobim December 15, 2010