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.
upvoted :-)
determined to solve this without looking into discussion board as it was mentioned "Easy".Took me couple of hours. Happy that I am not the only one in the boat.
Without having to looking into discussion I doubted after thinking about the logic considering the score and level of difficulty.
Yet , it goes true when I enter into Discussion.
Totally agree. I don't know how I got sidetracked, but spent the morning "fixing" a solution with a lot of states, and conditions, and always had a couple of tests failing.
Then started from scratch, and solved it with half the code in 20 minutes. It's not that complicated if you keep it simple :)
When I was debugging, I downloaded several test cases, but they were like thousands of characters long, inpossible to debug. I found some good short examples in the forum, and those helped with the debugging, but didn't got me far with the submissions.
After I reimplemented, I got it right right away. I didn't even have any "special cases". Later on I checked the editorial for official answer, the explanation was falwed, but the code was pretty similar to mine.
Two hints:
- don't forget the character in the midle if length is odd
- handle changes to 9s differently if a change is required at that position, or if it's an extra change of both character and mirror character
3 ifs. No else/else-ifs. But I used boolean-to-int tricks for counting. But such tricks are arithmetic - logically (for human) it is condition, but on CPU level it is just arithmetic.
Downvoted -- I solved it in under an hour. It wasn't that hard -- just needed to find the right test case. The test case from Svr_Aditya_Reddy (referencing abzalovim) should be sufficient to tell you how to do it.
8 4 1111911 => 91199119
And a case of my own for the other part: 3 2 329 => 999
I got 99911999 from input "9199919".Please tell me what i did wrong because according to question output will be highest palindromic number and 99911999 is greater than 9199919.
I do not agree. My solution does not contain more than 2 if's. The logic is very simple:
go through the half of the string with i idx, and compare s[i] with s[n-i-1]. If they differ, set both to the largest, increment the change count.
if the change count is gt than the allowed, quit with "-1"
otherwise go through again on the half of the string, and where it is not '9', replace it with '9', while incrementing the change count further until no more changes left. The only complicated part is that some numbers will be changed twice, those should be accounted for in a bit vector in the first loop.
if the string is of odd size, and there are any free changes left, replace the middle with '9'.
stringmakePalindrome(strings,intn,intk,boolodd,stringfirst,stringsecond,intdiff){stringresult;//rest will be number of extra changes we can do in palindromeintrest=k-diff;//this map will show if the index is included in making normal palindromeunordered_map<int,bool>included;for(inti=0;i<n/2;i++){if(first[i]!=second[n/2-i-1]){if(first[i]>second[n/2-i-1])second[n/2-1-i]=first[i];elsefirst[i]=second[n/2-1-i];included[i]=true;//include the index if it is changed}}for(inti=0;i<n/2;i++){//if the index is not included then rest will be deducted by 2if(first[i]!='9'&&!included[i]){rest-=2;if(rest<0){rest+=2;continue;}//setting the changed indexfirst[i]=second[n/2-i-1]='9';}//else by 1elseif(first[i]!='9'&&included[i]){rest--;if(rest<0){rest++;continue;}first[i]=second[n/2-i-1]='9';}}if(odd){if(rest)s[n/2]='9';}if(odd)result=first+s[n/2]+second;elseresult=first+second;returnresult;}// Complete the highestValuePalindrome function below.stringhighestValuePalindrome(strings,intn,intk){if(n==1){if(k>0)return"9";elseif(k==0)returns;elsereturn"-1";}stringfirst,second,result;intlen=n/2;boolodd;if(n%2==0)odd=false;elseodd=true;first.assign(s,0,n/2);if(odd)second.assign(s,n/2+1,n);elsesecond.assign(s,n/2,n);intdifferent=0;for(inti=0;i<n/2;i++){if(first[i]!=second[n/2-i-1]){different++;}}if(k<different)return"-1";elseresult=makePalindrome(s,n,k,odd,first,second,different);returnresult;}
Highest Value Palindrome
You are viewing a single comment's thread. Return to all comments →
I agree. This is outside of the "Easy" realm. And it's still July 31st here!
upvoted :-) determined to solve this without looking into discussion board as it was mentioned "Easy".Took me couple of hours. Happy that I am not the only one in the boat.
Without having to looking into discussion I doubted after thinking about the logic considering the score and level of difficulty. Yet , it goes true when I enter into Discussion.
Here are my few different solutions in Python,C++ & Java. 100% all test cases will pass
I hope you will find my answer useful.
Hackerrank Highest Value Palindrome Solution
Totally agree. I don't know how I got sidetracked, but spent the morning "fixing" a solution with a lot of states, and conditions, and always had a couple of tests failing.
Then started from scratch, and solved it with half the code in 20 minutes. It's not that complicated if you keep it simple :)
When I was debugging, I downloaded several test cases, but they were like thousands of characters long, inpossible to debug. I found some good short examples in the forum, and those helped with the debugging, but didn't got me far with the submissions.
After I reimplemented, I got it right right away. I didn't even have any "special cases". Later on I checked the editorial for official answer, the explanation was falwed, but the code was pretty similar to mine.
Two hints: - don't forget the character in the midle if length is odd - handle changes to 9s differently if a change is required at that position, or if it's an extra change of both character and mirror character
that hint was useful. thanks :)
I was stuck on that problem for the longest time until I saw your hint! Thank you :)
It's not an easy one!!
I have used 7 if's (0 else's), but there are 5 &&'s in the if-evaluates, so it is the equivalent of 12 if's
3 ifs. No else/else-ifs. But I used boolean-to-int tricks for counting. But such tricks are arithmetic - logically (for human) it is condition, but on CPU level it is just arithmetic.
https://www.hackerrank.com/challenges/richie-rich/submissions/code/38253930
You expressed all my feelings. :)
Downvoted -- I solved it in under an hour. It wasn't that hard -- just needed to find the right test case. The test case from Svr_Aditya_Reddy (referencing abzalovim) should be sufficient to tell you how to do it.
8 4 1111911 => 91199119
And a case of my own for the other part: 3 2 329 => 999
Compared to other challenges, marked as "Medium", this one is much easier.
Does the string have a leading 0? Because it has 7 digits, should be 8
Useful test cases, thanks, although there is an error in one of them: instead of 8 4 1111911 => 91199119 should be 8 4 1111911 => 9199919
I got 99911999 from input "9199919".Please tell me what i did wrong because according to question output will be highest palindromic number and 99911999 is greater than 9199919.
99911999 have length greater then input string which is 7
Sure, under a hour.
8 4 1111911 => 91199119 -->how come 7 letters become 8 here
in fact he likely meant 8 4 11119111, which does yield the final, or 7 3 1111911 => 9191919
but not sure how they are illuminating
it was in a interview and i was expected to solve this in 20 minutes i couldnt do it
I do not agree. My solution does not contain more than 2 if's. The logic is very simple:
i
idx, and compares[i]
withs[n-i-1]
. If they differ, set both to the largest, increment the change count."-1"
'9'
, replace it with'9'
, while incrementing the change count further until no more changes left. The only complicated part is that some numbers will be changed twice, those should be accounted for in a bit vector in the first loop.'9'
.what am i doing wrong here
It's funny to read your comment.
"My solution does not contain more than 2 if's"
Then you describe your solution with 4 ifs (implicitly).
Wow. Was this marked as "easy" for real? This was not easy!
Explaination in editorial is a bit wrong But the code is correct no issues there.
Simpe code with video explanation in c++ https://youtu.be/SlfyStPbW3E
Some test cases :
Answers:
It's probably not "easy", but it defeinitely doesn't require 15 ifs. Mine solution only has six and I cannot imagine where would I put nine more.