Finding the next palindrome for the given number

Hi Chandra, In Method 2, How would you handle a n… Vinay Sarang - Sep 6, 2014Hi Chandra, In Method 2, How would you handle a number with odd digits. Ex :: 31451 then output has to be 31513. Could you please share the algo for it. Thanks in advance, Vinay Keep the middle number somewhere, so the number will become, first+middle+reverse(first). Increment first until we get the right pallindrome. [Read More]

Finding the next palindrome for the given number

Problem Given a number, find the next smallest palindrome larger than this number. Example For example, if the input number is “2 3 5 4 5″, the output should be “2 3 6 3 2″. And if the input number is “9 9 9″, the output should be “1 0 0 1″. The input is assumed to be an array. Every entry in array represents a digit in input number. Let the array be ‘num[]‘ and size of array be ‘n’ [Read More]

How do I check if a number is a palindrome?

Note: First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. For the purpose of discussion here, we define negative integers as non-palindromes. Approaches to solve the problem Approach 1: The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate. [Read More]

Check If an Integer's Bit Representation Is a Palindrome

Question: how can you determine if a positive integer is a palindrome. For example, 5 is a palindrome because 5 = 101. Also, 9 is a palindrome because 9 = 1001. Solution: an integer is palindrome when its bit representation is the same reading from left to right or from right to left. Thus, in order to know if it is a palindrome or not, we just have to check if the number’s value is still the same when we read the number’s bits backward (right to left). [Read More]

Check if singly linked list is Palindrome

There are couple of solution to it : Method 1 - Using the stack A simple solution is to use a stack of list nodes. This mainly involves three steps. Traverse the given list from head to tail and push every visited node to stack. Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node. If all nodes matched, then return true, else false. [Read More]

Find longest palindrome in a string.

I cannot think of any that has a complexity less than O(n*n). one of the ways would be : 1. Take the string (s1) and its direct reversal (s2) 2. Slide s2 over s1 per character and count the number of similar characters starting from s2′s last character Hence, first: ———————— ABCDAMNMADGHJ JHGDAMNMADCBA Count =1 Then, ————————ABCDAMNMADGHJ –JHGDAMNMADCBA Count=0 . . . ————————ABCDAMNMADGHJ ————————JHGDAMNMADCBA Count=7 . . . ————————ABCDAMNMADGHJ Count= 0 We can exit at this point [Read More]

WAP to check whether string is palindrome

Problem Write a method which will accept a string and return true if the string is a palindrome and false if it isn’t. Follow up: a) your method should consider lower case and upper case characters to be the same. b) your method should ignore special characters and white spaces, for e.g. if your input were the strings were “Madam, I’m Adam!!”, then you should consider it a palindrome and hence return true ignoring case and special characters. [Read More]