Find the Closest Palindrome | Simple Observations | Leetcode 564 | codestorywithMIK

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • Whatsapp Community Link : www.whatsapp.c...
    This is the 42nd Video of our Playlist "Strings : Popular Interview Problems" by codestorywithMIK
    In this video we will try to solve a very good String Problem : Find the Closest Palindrome | Simple Observations | Leetcode 564 | codestorywithMIK
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Find the Closest Palindrome | Simple Observations | Leetcode 564 | codestorywithMIK
    Company Tags : YELP
    My solutions on Github(C++ & JAVA) - github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My Recursion Concepts Playlist : • Introduction | Recursi...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Summary :
    The solution aims to find the closest palindrome to a given number by generating several potential palindrome candidates. The steps are:
    Mirror the First Half: The first step is to create a palindrome by mirroring the first half of the given number.
    Generate Variants: Next, it generates two additional palindromes by mirroring (firstHalf + 1) and (firstHalf - 1) to handle cases where the nearest palindrome is slightly larger or smaller.
    Handle Edge Cases: The solution also considers edge cases, such as palindromes of the form 999... (e.g., 999) and 100...001 (e.g., 1001), which could be nearest palindromes for numbers like 1000.
    Find the Closest: Among the generated candidates, the closest palindrome is selected based on the smallest absolute difference from the original number. If multiple palindromes have the same difference, the smaller one is chosen.
    This approach ensures that all relevant possibilities are considered to find the nearest palindromic number efficiently.
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024

КОМЕНТАРІ • 95

  • @aswithasai4015
    @aswithasai4015 16 днів тому +9

    hello sir , got placed at microsoft beacuse of you thank you so much , I have been following your videos since you had 1k subscribers , I am a final year student of IIITA ,if you are coming for hiring drive would love to meet you in person once.

    • @codestorywithMIK
      @codestorywithMIK  16 днів тому +3

      First of all Congratulations 🎉.
      I can’t express my happiness when I see posts like these. So proud and happy for you.
      I am going to a different college for Hiring this time. May be we all will meet in future for sure. Stay connected over LinkedIn.
      Thank you for making my day. Congratulations 🙌 ❤️🙏😇

    • @aswithasai4015
      @aswithasai4015 16 днів тому +1

      @@codestorywithMIK thank you so much sir 😊.

    • @souravjoshi2293
      @souravjoshi2293 14 днів тому +1

      congrats

  • @jeehub041
    @jeehub041 20 днів тому +22

    Subah se wait kar rahe the sir aapka apna logic bana lia lekin jab tak ek baar aapka voice me explanation nahi sun leta din bekar sa lagta ha ❤❤

  • @satyam4565
    @satyam4565 19 днів тому +2

    The obly youtube channel which gives the INTUTION behind the problem all well known UA-camrs just give the method but no one tells about how they reach to this method. Thanks a lot

  • @Anime-ub7vs
    @Anime-ub7vs 19 днів тому +2

    Bhai contest ke solution ka bhi video dala kro. Becausr your explanation is best no one can explain like you. The way you tell the story of question is amazing it stick my brain permanent.

  • @paglikadamad7574
    @paglikadamad7574 18 днів тому +1

    Thank you ❤, your explanation skill is simply amazing.
    The problems others are struggling in making understandable, you made it very easy. Once again, thank you ❤.

  • @raisanjeeb42
    @raisanjeeb42 14 днів тому +1

    Good Solution bhaiya, Thank you for this wonderful approach and though process. I was frustrated with the edge cases but got a much clear idea by watching how you approached the question.

  • @YuktaTiwari
    @YuktaTiwari 19 днів тому +4

    I was literally waiting for your vdo 😭 Even my approach was similar to yours but I was not able to code but thanks to you. I got my mistakes and did learn a lot of things in this video. Thanks and I hope soon you'll get 100k subscribers 🎉.

  • @Avatar40456
    @Avatar40456 20 днів тому +10

    WAITING FOR THIS VEDIO FROM MORNING

  • @iayushsharma
    @iayushsharma 19 днів тому +1

    Indeed it is a good question on top of it this video contains the best explanation of this question. KUDOS to you MIK bhaiya. Thank you for making such great content. Whenever I come to this channel it feels like it is YT PREMIUM-level content.

  • @aayush8081
    @aayush8081 19 днів тому +1

    understood this problem at 3.50 am and submitted . saved my streak .

  • @arjunkumaryadav8102
    @arjunkumaryadav8102 19 днів тому +2

    I used the same approach sir and got stuck at the same test case...sad to say😢....but I am happy that Now I am able to think like u....and began to approach the problem in the same

  • @study-yd6es
    @study-yd6es 16 днів тому +1

    Keep posting regular videos !!! Amazing Content

  • @be-sreenu2202
    @be-sreenu2202 20 днів тому +6

    waiting for this bruhh!!

  • @____abhay_sharma___
    @____abhay_sharma___ 16 днів тому +1

    Sir pls make a video on 3086: In no of moves to pick k 1's it is a hard labelled sliding window questions and i am sure you will make this an easy problem

  • @nipamparmar3872
    @nipamparmar3872 12 днів тому

    A request to upload the contest solutions regularly... Will be very helpful

  • @AmarjeetKumar-to9ub
    @AmarjeetKumar-to9ub 19 днів тому +2

    lots of learning
    tysm :)

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 19 днів тому +1

    NICE SUPER EXCELLENT MOTIVATED

  • @tharunkumar8133
    @tharunkumar8133 20 днів тому +1

    Great explanation as always.. CFBR!!

  • @rockykumarverma980
    @rockykumarverma980 19 днів тому +1

    thank you Bhaiya ji🙏🙏🙏

  • @SHIVAMOJHA21
    @SHIVAMOJHA21 20 днів тому +4

    badi derr kardi meherbaan aate aate!

  • @kaustubhchaudhari9838
    @kaustubhchaudhari9838 16 днів тому

    Kaha ho Bhai? Leetcode daily challenge Missing you. You are the hope to solve it Man, Come back

    • @codestorywithMIK
      @codestorywithMIK  16 днів тому +2

      Hi there, leetcode POTD was already there in my playlist.
      ua-cam.com/video/zTM9k6jqpXI/v-deo.htmlsi=5NjaJa-buUDgme-o
      ❤️🙏😇

  • @danishdk1804
    @danishdk1804 19 днів тому

    Sir, if possible(If you have time), please start a series of contest solutions as well.😔🙏

  • @pranavjawale01
    @pranavjawale01 19 днів тому

    fully understood

  • @rohan8758
    @rohan8758 19 днів тому

    Amazing explnation Mik.

  • @_hustler_boy
    @_hustler_boy 10 днів тому

    bhaiya aap list provide krwa doge please kitne questions enough krne ko maine 200+ questions kiye h concept bhi smjh me aate h kuch questions abhi bhi rh jate h wo easy medium hard koi bhi hota h

  • @abhisheksinghdangi5027
    @abhisheksinghdangi5027 19 днів тому

    bhot wait kraya bhaiya ... chlo koi ni ... travelling me to chlta h

  • @souravjoshi2293
    @souravjoshi2293 14 днів тому

    epic explanation

  • @TheAI-Tutor
    @TheAI-Tutor 19 днів тому

    Doubt- How should we think about the TCs in an intv? I feel I am v dependant upon LC's Compiler to check for everything rather than using my own brain....Pls guide.

  • @neelakshisoni6118
    @neelakshisoni6118 13 днів тому

    Hey Mike, I am still confused , how this mirror image trick is working for all the inputs? what is the logic behind that ? I was asked thsi question for amazon SDE 2 role, but was not abel to solve , I have given o(n) solution , but interviewer was looking for in constant time.

  • @pawanprajapati2618
    @pawanprajapati2618 20 днів тому +2

    Bhaiya please ek video C++ STL par bna dijiye, please bhaiya 🙏🙏🙏🙏

  • @anikbiswas8
    @anikbiswas8 19 днів тому

    class Solution {
    int func(long firstHalf,bool isEven){
    long resultNum=firstHalf;
    if(!isEven){
    firstHalf/=10;
    }
    while(firstHalf){
    int digit=firstHalf%10;
    resultNum=resultNum*10+digit;
    firstHalf/=10;
    }
    return resultNum;
    }
    public:
    string nearestPalindromic(string n) {
    int l=n.length();
    int mid=l/2;
    int firstHalfLength=(l%2==0)?mid:(mid+1);
    long firstHalf=stol(n.substr(0,firstHalfLength));
    vectorpossibleResults;
    possibleResults.push_back(func(firstHalf,l%2==0));
    possibleResults.push_back(func(firstHalf+1,l%2==0));
    possibleResults.push_back(func(firstHalf-1,l%2==0));
    possibleResults.push_back((long)pow(10,l-1)-1);
    possibleResults.push_back((long)pow(10,l)+1);
    long diff=LONG_MAX;
    long result=INT_MAX;
    long originalNum=stol(n);
    for(long num:possibleResults){
    if(num==originalNum) continue;
    if(abs(num-originalNum)

  • @rajchinagundi7498
    @rajchinagundi7498 19 днів тому

    MIK sir, leetcode contest 412 Q3 & Q4 please video upload kro. Felt dumbfounded when it came for optimising.

  • @DurgaShiva7574
    @DurgaShiva7574 19 днів тому

    what about inputs like 262, or 505, 666, how will our code handle these ?

  • @techmaster9953
    @techmaster9953 19 днів тому

    pls solve contest questions also..

  • @ManishKumar-uc1rw
    @ManishKumar-uc1rw 17 днів тому

    Rank teams by vote wale questions me doubt ek video uspar banao na pls sir

  • @gauravbanerjee2898
    @gauravbanerjee2898 19 днів тому

    Thanks a lot bhaiya ❤❤

  • @supriyakashyap3295
    @supriyakashyap3295 19 днів тому

    hello sir, can you please upload the solution to a very popular problem? 152. Maximum Product Subarray. There is no available solution on UA-cam that I can understand. I can see solution and know its working by dry running it, but not able to understand why its working. Please if you have time can you make a video on it?🙏

  • @manishtomar2713
    @manishtomar2713 16 днів тому

    hello sir, new subscriber here..can u please make a video on this leetcode problem 1000. Minimum Cost to Merge Stones.. Thank you..

  • @sogarriz
    @sogarriz 19 днів тому

    i also thought how aboout handling the 999... type and 101 type edge cases like +2 and -2 instead of complexing with 10^x -1 ? does that have a flaw im not considering?

  • @abhisheksinghdangi5027
    @abhisheksinghdangi5027 19 днів тому

    Exactly mujhe m bhi yhi lga tha ki bs beech se tod deta hu bro ... pr bad m testcase 19 aa gya ... Holy momos

  • @amitmehraa
    @amitmehraa 20 днів тому

    Hello mik, can you make a video on roadmap to dsa, like how to learn dsa step by step, topic by topic, concept by concept so that it becomes easy to learn harder topic

  • @ugcwithaddi
    @ugcwithaddi 20 днів тому +2

    Finally 😃

  • @tanishagupta9997
    @tanishagupta9997 19 днів тому

    Please make a video explaining today's contest solution

    • @codestorywithMIK
      @codestorywithMIK  19 днів тому +2

      I am travelling actually.
      Will try to upload this week 🙏😇

  • @RahulYadav-ho3ts
    @RahulYadav-ho3ts 19 днів тому

    What is the software that are you using for the explaination

  • @dipendrajawariya4115
    @dipendrajawariya4115 19 днів тому

    will you be solving today's weekly contest problem??

    • @codestorywithMIK
      @codestorywithMIK  19 днів тому

      Actually i am travelling. I will try make videos this week on the contest

    • @dipendrajawariya4115
      @dipendrajawariya4115 18 днів тому

      @@codestorywithMIK will be waiting nd tried alot of videos but the intuition part is mission....

  • @janaSdj
    @janaSdj 19 днів тому

    nice...

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz 20 днів тому +1

    Bhaiya mere dimag mein pehele aaya tha, ki given string ko number mein convert koro, uske band do variable create Kara, ek given num - 1 and dusre given num + 1, and create a function to check palindrome, and call for both numbers, if both are palindrome then return minimum else return that number which is palindrome among both
    I know that it is ghatia approach, can anyone thought about this also? Please say

    • @331sachinkiragi8
      @331sachinkiragi8 20 днів тому +1

      That's not a ghatia approach.
      It's a common sense approach

    • @smile-zu2fg
      @smile-zu2fg 20 днів тому

      ​@@331sachinkiragi8 Ya I tried same approach but not passed all the test cases 😢

    • @RishabhChatterjee-fg2gz
      @RishabhChatterjee-fg2gz 20 днів тому

      @@331sachinkiragi8 Thanks bro for motivating

    • @RishabhChatterjee-fg2gz
      @RishabhChatterjee-fg2gz 20 днів тому

      @@smile-zu2fg yeah, INT_MAX mein runtime error de diya, out_of_range

  • @buzzdhamal2550
    @buzzdhamal2550 20 днів тому +18

    dry run me ek mistake hogaya hai 10^(n-1)+1 ....... 101 ki jagah 1001 likh diye aap galti se

    • @codestorywithMIK
      @codestorywithMIK  20 днів тому +12

      Thank you ❤️
      And apologies for silly mistake

    • @gui-codes
      @gui-codes 19 днів тому +1

      yep. samajh gaya tha mai.

    •  19 днів тому

      at what timestamp

    • @kartiknegi6134
      @kartiknegi6134 19 днів тому +2

      But he is correct. It should be 10^(n) +1. for the case of 999, it would be 10^3 +1 which is indeed 1001.

    • @user-ub2is4rs4x
      @user-ub2is4rs4x 19 днів тому

      Correct hai i think . But koi na, mai samajh gaya

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 20 днів тому

    public String nearestPalindromic(String n){
    long num=Long.valueOf(n);
    long small=smallest(String.valueOf(num-1));
    long large=largest(String.valueOf(num+1));
    if(large-num

  • @ayushigholase1228
    @ayushigholase1228 20 днів тому +2

    1551.Minimum operations to make Array Equal
    Can you make a video for this I have encountered many Question like this
    But don't understand what to do
    There are many other question like this but I don't understand from how to start what is the approach
    Can you simply and also tell the general approach for solving this type of question please.

    • @devanshbhawsar4768
      @devanshbhawsar4768 19 днів тому

      Well this is question which can be done by many approaches
      And one of the solution is
      Return (n*n)/4 ; 😂 which is O(1) T.C.

  • @sahilshrivastava6455
    @sahilshrivastava6455 17 днів тому

    I
    guess space complexity is O(N)

  • @user-hg8hm1xm7q
    @user-hg8hm1xm7q 19 днів тому

    bhaiya please try to upload the video at morning

  • @shabananoor9423
    @shabananoor9423 20 днів тому

    ❤❤

  • @shivamjain9968
    @shivamjain9968 19 днів тому

    har 2 min m ad kyu ara hai

  • @GateDA-omkarpunjaji
    @GateDA-omkarpunjaji 19 днів тому

    typedef long long ll;
    class Solution {
    public:
    ll solve(ll &num){
    string s = to_string(num);
    ll n = s.length();
    ll l = (n-1)/2 , r = n/2;
    while(l>=0){
    s[r++] = s[l--];
    }
    return stoll(s);
    }
    ll next(ll & num){ // just greater
    ll l = num, r = 1e18;
    ll res = INT_MAX;
    while(l num){
    res = palin;
    r = mid-1;
    }
    else
    l = mid+1;
    }
    return res;
    }
    ll prev(ll &num){ // just smaller
    ll l = 0;
    ll r = num;
    ll res = INT_MAX;
    while(l

  • @331sachinkiragi8
    @331sachinkiragi8 20 днів тому

    Bro thoda jaldi daala karo na video

  • @hellsterkun8764
    @hellsterkun8764 16 днів тому

    Sir please check you mail!
    Thanks

  • @harshilgupta5179
    @harshilgupta5179 20 днів тому

    what about numbers like 18 ,10 15 ,7,6 they wont follow your logic

    • @hiteshpatwal9688
      @hiteshpatwal9688 19 днів тому

      Sach me

    • @gui-codes
      @gui-codes 19 днів тому +2

      It will work bro.
      Try doing dry run fror 18
      We will have these possibilities as per the logic MIK explained -
      11
      22
      0
      9
      101
      We will then choose closest 22 will be the output.

    • @harshilgupta5179
      @harshilgupta5179 19 днів тому

      18 ko karenge toh 11 ayega fir 22 kaise ayega

    • @rajeshkumar-ws5ku
      @rajeshkumar-ws5ku 19 днів тому +2

      in even case we take half of the element
      in case 18
      we take 1 as middle element ans also right same element in right so element becomes 11 ,this is first scenario
      second scenario
      we take 1 as a middle element ans increase the middle element by 1, so element becomes 2 and also take its right element so our element becomes 22
      third scenario
      we take 1 as middle element and decrease by 1 so element becomes 0 as take its right element so element becomes 00
      we take [22,11,00] we choose closet to 18 which is 22 is right answer.
      i hope we now understand it.

  • @nishant4861
    @nishant4861 19 днів тому

    why 999 ans is 1001 but smallest is 989

    • @priyaaofficial7
      @priyaaofficial7 19 днів тому

      1001-999=2
      999-989=10
      It's all about the closest number by using the difference between them, i hope you will understand it.

    • @nishant4861
      @nishant4861 19 днів тому

      @@priyaaofficial7 ya got it

    • @nishant4861
      @nishant4861 19 днів тому

      @@priyaaofficial7 finally complete huaa

  • @devanshbhawsar4768
    @devanshbhawsar4768 20 днів тому +2

    With brute force used long but only 163/217 test cases passed 🥲

  • @Engineering.Wallah
    @Engineering.Wallah 20 днів тому

    Bhai cp kru?

    • @omkumarjha2745
      @omkumarjha2745 18 днів тому

      You tube to kar nhi paa raha CP kaha se karenga.

    • @Engineering.Wallah
      @Engineering.Wallah 18 днів тому

      @@omkumarjha2745 apne channel pr dhyan de

    • @omkumarjha2745
      @omkumarjha2745 18 днів тому

      @@Engineering.Wallah Nhi de paaya isliye chor diya

  • @bhanuburman3289
    @bhanuburman3289 19 днів тому

    It means that these types of problems cannot be solved without knowing the testcase which is not possible to see during actual examination 🥲

  • @muntajir646
    @muntajir646 20 днів тому +1