-1

Given a string representing the starting number and a maximum number of changes allowed, create the largest palindromic string of digits possible or the string -1 if it's impossible to create a palindrome under the contstraints.

I wrote a code who answer on the questions, but i have an error that i dont know where it is, or if even the code work.

static String highestValuePalindrome(String s, int n, int k) {

        for(int i =0 ; i < n ; i++){
            char[] ch =s.toCharArray();   

            if(n==1)
            return s ; 
            else if ((ch[i] != ch[n-i-1]) && (k != 0) ){
                ch[i] = ch[n-i-1] = 9 ; 
                k--;
            }
        }
        String str = new String(ch);
return str ; 
    }

Output Format

Print a single line with the largest number that can be made by changing no more than digits. If this is not possible, print -1.

Sample Input n=4, k=1 3943 Sample Output 3993

Sample Input n=6, k=3 092282 Sample Output 992299

Sample Input n=4, k=1 0011 Sample Output -1

Nicholas
  • 39
  • 6

1 Answers1

1

First of all there is no need to pass n as a parameter because it's just the length of the string. Secondly, this is not the complete program. I have made many changes to the given code.

public class largestPalindorme {
public static void main(String[] args) {
    System.out.println(highestValuePalindrome("0011", 1));
}
static String highestValuePalindrome(String s, int k) {
    char[] ch = s.toCharArray();
    int n = s.length(); // which is same as n which you passed as parameter
    int minChangesRequired = MinumumChangesNeeded(s);
    //if the changes required to make the given string a palindrome is less then k then only it will go inside or it will return -1
    if (k >= minChangesRequired) {
        int diff = 0;
        if (k > minChangesRequired) {
            diff = k - minChangesRequired;
            for (int l = 0; l < diff; l++) {
                ch[l] = '9';
                ch[n - l - 1] = '9';
            }
        }
        for (int i = diff; i < n - diff / 2; i++) {
            if (ch[i] != ch[n - i - 1]) {
                //if checks which number is greater
                int greater = Integer.parseInt(String.valueOf(ch[i])) > Integer.parseInt(String.valueOf(ch[n - i - 1])) ? Integer.parseInt(String.valueOf(ch[i])) : Integer.parseInt(String.valueOf(ch[n - i - 1]));
                //replaces the smaller number from the greater number.
                if (Integer.parseInt(String.valueOf(ch[i])) != greater) {
                    ch[i] = ch[n - i - 1];
                } else {
                    ch[n - i - 1] = ch[i];
                }
            }
        }
        String str = new String(ch);
        return str;
    }
    return "-1";
}
//this function returns the minimum changes we need to do to make it a palindrome.
public static int MinumumChangesNeeded(String s) {
    int count = 0;
    char[] ch = s.toCharArray();
    int n = s.length();
    for (int i = 0; i < n / 2; i++) {
        if (ch[i] != ch[n - i - 1]) {
            count++;
        }
    }
    return count;}}
jay patel
  • 21
  • 5