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.
/* * Complete the 'highestValuePalindrome' function below. * * The function is expected to return a STRING. * The function accepts following parameters: * 1. STRING s * 2. INTEGER n * 3. INTEGER k */void_print(constset<size_t>&v){for_each(v.begin(),v.end(),[](intt){cout<<t<<" ";});cout<<endl;}set<size_t>_f(conststring&s){set<size_t>rx;size_tn=s.size();size_tk=static_cast<size_t>(floor(n/2));size_tj;for(size_ti=0;i<k;++i){j=n-1-i;if(s[i]!=s[j]){rx.insert(i);}}returnrx;}charlx(charc1,charc2){return(c1>c2)?c1:c2;}stringhighestValuePalindrome(strings,intn,intk){if(k>=n){returnstring(n,'9');}size_ti,j;autoix=_f(s);// _print(ix);if(ix.size()>k){return"-1";}for(autoii:ix){j=n-1-ii;autoc=lx(s[ii],s[j]);s[ii]=c;s[j]=c;k--;}i=0;j=n-1-i;while((i<j)&&(k>0)){if(s[i]!='9'){if((ix.find(i)!=ix.end())&&(k>=1)){s[i]='9';s[j]='9';k--;}elseif(k>=2){s[i]='9';s[j]='9';k-=2;}}i++;j--;}if((k>0)&&(n%2==1)){j=static_cast<size_t>((n-1)/2);s[j]='9';k--;}returns;}
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 →