We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
We are first checking: left and right values and moving towards the middle in a while loop… if something does NOT match, we make the change and record that change in our Processed array (i.e. this will tell us what INDEX has been changed) AND when we make a change we decrement value of K - 1; because K tracks how many changes we can make, so we are working backwards
ones we created palindrome and made and recorded the changes in the Processed array, we need to check if K > 0, because we can now make further changes
so if K > 0 we reset taht values of our pointers i(left) and j(right)
while i <= j AND k> 0 we run the new while loop
if str[i] !== 9 we can make a change here
we first check has ANY of the values either left or right been changed? If YES, we make 1 change, and set both str[i] and str[j] to 9 and K - 1; cause 1 change if NOT, we check if K value is bigger than > 1, if so, we make str[i] and str[j] to 9 and K - 2, because we have now made 2 changes.
at the end of every while loop we i++; j = j - 1;
on second while we have end condition that if i === j && k >=1 - i.e. we can still make 1 more change, then str[i] = 9; break; this is for test case 6, where there is middle value and string is made up for 5 characters; because at this point we are in DEAD MIDDLE, so we need to set this middle value to 9; we can do that by either str[i] = 9; OR str[j]=9 makes no difference, since both i and j are dead middle at this stage
finally, just return: s.join(‘’) and it works
function highestValuePalindrome(s, n, k) {
if (n === 1){
return k == 1 ? '9' : '-1'
}
let firstEl = s[0];
if(k===0 && [...s].every(el=> el===firstEl)){
console.log('here')
return s;
}
let i = 0;
let j = n-1;
s = Array.from(s, el=> Number(el));
console.log(s);
let processed = Array(n).fill(false);
console.log("processed: ", processed);
while(i <= j){
if(k===0){
return '-1'
}
if(s[i] > s[j]){
s[j] = s[i];
processed[j] = true;
k--;
}
if(s[i] < s[j]){
s[i] = s[j];
processed[i] = true;
k--;
}
i++;
j--;
// end of while loop 1
}
console.log(s);
console.log(processed);
console.log(k);
// now handle any remaining conditions
if(k > 0){
i = 0;
j = n-1;
while(i <=j && k > 0){
if(s[i] != 9){
if(processed[i] === true || processed[j] === true){
s[i]=9;
s[j]=9;
k--;
} else {
if(k > 1){
s[i]=9;
s[j]=9;
k = k - 2;
}
}
}
i++;
j--;
if(i===j && k >=1){
s[i]=9;
break;
}
// end of while loop 2
}
}
console.log(s.join(''));
console.log(processed);
console.log(k);
return s.join('')
// ! function end
}
let s1 = '3943';
let n1 = 4;
let k1 = 1; // 3993;
let s2 = '777';
let n2 = 3;
let k2 = 0;
highestValuePalindrome(s2,n2,k2);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Highest Value Palindrome
You are viewing a single comment's thread. Return to all comments →
JS:
Keys:
function highestValuePalindrome(s, n, k) {
}
let s1 = '3943'; let n1 = 4; let k1 = 1; // 3993;
let s2 = '777'; let n2 = 3; let k2 = 0;
highestValuePalindrome(s2,n2,k2);