Highest Value Palindrome

  • + 14 comments
    [deleted]
    • + 1 comment

      I agree. This is outside of the "Easy" realm. And it's still July 31st here!

      • + 2 comments
        [deleted]
        • + 1 comment

          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.

          • + 1 comment
            [deleted]
            • + 0 comments

              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.

        • + 0 comments

          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

    • + 1 comment

      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 :)

      • + 1 comment
        [deleted]
        • + 2 comments

          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

          • + 0 comments

            that hint was useful. thanks :)

          • + 0 comments

            I was stuck on that problem for the longest time until I saw your hint! Thank you :)

    • + 0 comments

      It's not an easy one!!

    • + 1 comment

      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

      • + 1 comment
        [deleted]
        • + 1 comment

          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

    • + 0 comments

      You expressed all my feelings. :)

    • + 6 comments

      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

      • + 1 comment
        [deleted]
        • + 0 comments

          Compared to other challenges, marked as "Medium", this one is much easier.

      • + 0 comments

        Does the string have a leading 0? Because it has 7 digits, should be 8

      • + 1 comment

        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

        • + 1 comment

          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.

          • + 0 comments

            99911999 have length greater then input string which is 7

      • + 0 comments

        Sure, under a hour.

      • + 1 comment

        8 4 1111911 => 91199119 -->how come 7 letters become 8 here

        • + 0 comments

          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

    • + 1 comment

      it was in a interview and i was expected to solve this in 20 minutes i couldnt do it

    • + 4 comments

      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'.
      • + 0 comments

        what am i doing wrong here

        string makePalindrome(string s, int n, int k,bool odd,string first,string second,int diff){
            string result;
        
            //rest will be number of extra changes we can do in palindrome
            int rest=k-diff;
        
            //this map will show if the index is included in making normal palindrome
            unordered_map<int,bool> included;
        
            for(int i=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];
                    else
                        first[i]=second[n/2-1-i];
                    included[i]=true;   //include the index if it is changed
                }
            }
        
            for(int i=0;i<n/2;i++){
                //if the index is not included then rest will be deducted by 2
                if(first[i]!='9' && !included[i]){
                    rest-=2;
                    if(rest<0){
                        rest+=2;
                        continue;
                    }
                    //setting the changed index
                    first[i]=second[n/2-i-1]='9';
                }
                //else by 1
                else if(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;
            else
                result=first+second;
        
            return result;
        }
        
        // Complete the highestValuePalindrome function below.
        string highestValuePalindrome(string s, int n, int k) {
            if(n==1){
                if(k>0)
                    return "9";
                else if(k==0)
                    return s;
                else
                    return "-1";
            }
            
            string first,second,result;
            int len=n/2;
            bool odd;
            if(n%2==0)
                odd=false;
            else
                odd=true;
        
            first.assign(s,0,n/2);
            if(odd)
                second.assign(s,n/2+1,n); 
            else
                second.assign(s,n/2,n);
            
            int different=0;
            for(int i=0;i<n/2;i++){
                if(first[i] != second[n/2-i-1]){
                    different++;
                }
            }
        
            if(k<different)
                return "-1";
            else
                result=makePalindrome(s,n,k,odd,first,second,different);
        
            return result;
        } 
        
      • + 0 comments

        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).

    • + 0 comments

      Wow. Was this marked as "easy" for real? This was not easy!

    • + 0 comments

      Explaination in editorial is a bit wrong But the code is correct no issues there.

    • + 0 comments

      Simpe code with video explanation in c++ https://youtu.be/SlfyStPbW3E

    • [deleted]
      + 0 comments

      Some test cases :

      4 1
      1321
      4 2
      1321
      4 3
      1321
      4 2
      9329
      5 1
      13121
      5 2
      13121
      5 3
      13121
      5 4
      13121
      5 3
      93129
      

      Answers:

      1331
      1991
      9339
      9999
      13131
      19191
      93139
      99199
      99999
      
    • + 0 comments

      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.