Javatpoint Logo
Javatpoint Logo

Largest Palindrome by Changing at Most K-digits in Java

Given an input number and an integer K, the task is to find the largest palindrome that can be obtained by changing at most K digits. The modifications involve replacing a digit in the number with another digit, but the total number of changes must not exceed K. The problem requires developing an algorithm to efficiently identify the largest palindrome by strategically choosing the digits to be modified while staying within the given constraint.

Example 1:

Input: "12321"

K:2

Output: Largest Palindrome:"12321"

Explanation: Taken input "12321" and we should change at most k digits that is 2 digits. However, the string input which is already a palindrome so no changes are required.

Example 2:

Input:"45678"

K:3

Output: Largest Palindrome: "87678"

Explanation: Taken input "45678" and we should change at most k digits that is 3 digits. However, to make a largest palindrome we must change the positions for getting lexicographically largest palindrome after three changes is "87678".

Example 3:

Input:"12345"

K:1

Output: Largest Palindrome: "Not Possible"

Explanation: Taken input is "12345" and we should change at most k digits that is 1 digit. However, it is not possible to make the palindrome by changing three digits in this case and the string does not contain any repeated digits and by changing three digits result in the lexicographically smaller in number.

Approach 1: Using Two pointers

The approach involves using two pointers, one starting from the start of the string and the alternative from the cease. Compare the characters on the corresponding positions and if they are not identical we should replace one of the characters to cause them to same. Keep track of the number of changes made and continue until the pointers meet or the maximum number of changes is reached.

ALGORITHM:

Step 1: Convert the input string str into a character array chars.

Step 2: Initialize two pointers left and right to the start and end of the character array respectively.

Step 3: Iterate while loop from left is less than or equal to right:

Step 3.1: If the characters at left and right positions are not equal:

Step 3.1.1: Check if no more changes are allowed (k reaches 0). If so, return "Not Possible".

Step 3.1.2: Choose the larger character between chars[left] and chars[right] and update both positions with the chosen character.

Step 3.1.3: Decrement the number of available changes k.

Step 3.2: Move left pointer one step forward and right pointer one step backward.

Step 4: Convert the character array chars back to a string.

Step 5: Return the resulting largest palindrome.

Implementation:

The implementation of the above steps is given below

Output:

Example 1:
Input: 12321, k = 2
Largest Palindrome: 12321
Example 2:
Input: 45678, k = 3
Largest Palindrome: 87678
Example 3:
Input: 12345, k = 1
Largest Palindrome: Not Possible

Complexity Analysis:

Time Complexity: O(n)-The algorithm iterates through the character array once, comparing and potentially changing characters.

Space complexity: O(n)-The algorithm uses extra space for storing the character array chars it has the equal to the length of the input string n.

Approach 2: Using Recursion

The approach involves recursively exploring all possible combinations of changing digits to find the largest palindrome. Start from the leftmost and rightmost positions of the string and try changing digits until the maximum number of changes is reached or a palindrome is formed.

ALGORITHM:

Step 1: Create a variable largestPalindrome to store the largest palindrome found.

Step 2: Invoke the findLargestPalindrome method with the input string str and the maximum number of digit changes k.

Step 3: Inside the findLargestPalindrome method:

Step 3.1: Initialize largestPalindrome as an empty string.

Step 3.2: Invoke the largestPalinRec method to perform recursion on the character array representation of str.

Step 3.3: If largestPalindrome is still empty after recursion, return "Not Possible" indicating that it is not possible to form a palindrome by changing at most k digits.

Step 3.4: Otherwise, return largestPalindrome.

Step 4: The largestPalinRec method:

Step 4.1: If the left pointer left is greater than or equal to the right pointer right it means that the entire string has been processed now Assign largestPalindrome with the current character array.

Step 4.2: If the characters at left and right are equal, move the pointers inward and continue recursion.

Step 4.3: If the characters at left and right are not equal, check if there are remaining digit changes (k > 0).

Step 4.3.1: Choose the larger character between the two and update both positions.

Step 4.3.2: Decrement k by 1.

Step 4.3.3: Move the pointers inward and continue recursion.

Step 5: In the main method, three examples are provided:

Step 5.1: Example 1: Input string is "12321" and k is 2.

Step 5.2: Example 2: Input string is "45678" and k is 3.

Step 5.3: Example 3: Input string is "12345" and k is 1.

Step 5.4: The largest palindromes for each example are printed.

Implementation:

The implementation of the above steps is given below

Output:

Example 1:
Input: 12321, k=2
Largest Palindrome:12321
Example 2:
Input:45678,   k=3
Largest Palindrome: 87678
Example 3:
Input: 12345, k=1
Largest Palindrome: Not Possible

Complexity Analysis:

Time Complexity: The time complexity of this algorithm is O(N), where N is the length of the input string str. It is because we need to process each character in the string exactly once during recursion.

Space Complexity: The space complexity is O(N) as well. It is due to the space used to store the character array representation of the string and the recursive call stack.







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