Javatpoint Logo
Javatpoint Logo

Next Smallest Palindrome problem in Java

given a string n that represents an integer, our task is to find the palindrome and return the closest integer (excluding itself). In the instance of a tie, give the one that is smaller as a return. The absolute difference between two integers is minimized to determine which is closest.

Example 1:

Input:

int N = 1000

Output:

The next smallest palindrome is 1001.

Explanation:

For the given number, the next smallest number is 1001, which is also a palindrome. Hence, the next smallest palindrome is 1001.

Example 2:

Input:

int N = 8888

Output:

The next smallest palindrome is 8998.

Explanation:

For the given number, the next smallest number is 8998, which is also a palindrome. Hence, the next smallest palindrome is 8998.

Example 3:

Input:

int N = 1331

Output:

The next smallest palindrome is 1441.

Explanation:

For the given number, the next smallest number is 1441, which is also a palindrome. Hence, the next smallest palindrome is 1441.

As we see in the above three examples, there are three different kinds of inputs, particularly that we have taken.

  1. The input number is a palindrome with identical digits in the number (Example 2).
  2. The input integer is not a palindrome (Example 1).
  3. The input number is a palindrome with all distinct digits in the number (Example 3)

Approach: Nave Approach

In order to convert it to a palindrome, we can either take the left or right side mirror. It is not a given that the palindrome created from taking the right side's mirror will be the next larger palindrome. Therefore, we need to duplicate the left side's mirror to the right side. However, there are different circumstances that call for distinct approaches. Consider a peek at the following actions. The two indices, i and j, will be used to start. i will point to the two middle elements, or if n is odd, will point to two elements surrounding the middle element. i and j are moved apart one at a given time by us.

Algorithm:

Step 1: Initially declare the function isPalindrome. The input parameter is an integer N, and the return value is 1 if N is a palindrome and 0 otherwise.

Step 2: Initialize the variables num, k, and reverse to store, respectively, the input N's number, digit, and reverse.

Step 3: Add N's initial value into num.

Step 4: Reverse the N digits using a while loop, then store the outcome in reverse.

Step 5: Determine whether the initial number, num, equals its reverse. Return 1 if they are equal; return 0 otherwise.

Implementation:

FileName: NextPalindromeNaive.java

Output:

The smallest Next Palindrome is :1001

Output:

The Time Complexity: O(N * |N|) where 'N' represents the iput number taken and the Space Complexity: O(1)

Approach: Efficient Method

This approach includes an algorithm that finds out the next palindrome that is larger than a given number in an efficient manner. Iteratively traversing over the input number's digits, it checks for palindrome qualities and determines whether any changes are required. When carrying over is required, it uses a recursive method for addressing it. In order to retain the integrity of the palindrome, it is important to confirm whether the number is already a palindrome, determine which digits need to be increased or decreased, and modify the number accordingly to guarantee that the next palindrome is formed.

Algorithm:

Step 1: Declare a function called "addingOne" for representing the number as a string using stringbuilder.

Step 2: Initialize two variables that represent the number, and the index of the digit should be incremented.

Step 2.1: It appends '1' to the start and finish of the StringBuilder if the index falls below 0, signaling the need for an extra digit.

Step 2.2: It sets the digit at the current index to '0' and then calls itself recursively for the next digit if it is '9'.

Step 2.3: If not, it increases the current digit and mirrors it to the last digit that corresponds to it.

Step 3: Initialize a StringBuilder that keeps the number and variables to track palindrome attributes.

Step 4: Iterating through half of the input number's digits, it checks for palindrome qualities and determines if any modifications are required.

Step 5: In the event that the number is already a palindrome, the middle or middle-1 digit, depending on the length of the number, is increased by calling the addingOne function.

Step 6: Additionally, it runs the addingOne function to increment the relevant digit if the integer is not a palindrome and no subtraction is required.

Step 7: The computed palindrome is finally returned as a string.

Implementation:

FileName: EfficientNextPalindrome.java

Output:

The next smallest palindromic value is: 1001

Complexity Analysis:

The above code's time complexity is O(N), and its space complexity is O(N), where N represents the length of the input string.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA