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
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.
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 🙌 ❤️🙏😇
@@codestorywithMIK thank you so much sir 😊.
congrats
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 ❤❤
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
true
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.
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 ❤.
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.
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 🎉.
WAITING FOR THIS VEDIO FROM MORNING
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.
understood this problem at 3.50 am and submitted . saved my streak .
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
Keep posting regular videos !!! Amazing Content
waiting for this bruhh!!
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
A request to upload the contest solutions regularly... Will be very helpful
lots of learning
tysm :)
NICE SUPER EXCELLENT MOTIVATED
Great explanation as always.. CFBR!!
thank you Bhaiya ji🙏🙏🙏
badi derr kardi meherbaan aate aate!
mazhar namhai
Kaha ho Bhai? Leetcode daily challenge Missing you. You are the hope to solve it Man, Come back
Hi there, leetcode POTD was already there in my playlist.
ua-cam.com/video/zTM9k6jqpXI/v-deo.htmlsi=5NjaJa-buUDgme-o
❤️🙏😇
Sir, if possible(If you have time), please start a series of contest solutions as well.😔🙏
fully understood
Amazing explnation Mik.
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
bhot wait kraya bhaiya ... chlo koi ni ... travelling me to chlta h
epic explanation
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.
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.
Bhaiya please ek video C++ STL par bna dijiye, please bhaiya 🙏🙏🙏🙏
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)
MIK sir, leetcode contest 412 Q3 & Q4 please video upload kro. Felt dumbfounded when it came for optimising.
what about inputs like 262, or 505, 666, how will our code handle these ?
pls solve contest questions also..
Rank teams by vote wale questions me doubt ek video uspar banao na pls sir
Thanks a lot bhaiya ❤❤
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?🙏
hello sir, new subscriber here..can u please make a video on this leetcode problem 1000. Minimum Cost to Merge Stones.. Thank you..
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?
Exactly mujhe m bhi yhi lga tha ki bs beech se tod deta hu bro ... pr bad m testcase 19 aa gya ... Holy momos
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
Finally 😃
Please make a video explaining today's contest solution
I am travelling actually.
Will try to upload this week 🙏😇
What is the software that are you using for the explaination
Apple notes
will you be solving today's weekly contest problem??
Actually i am travelling. I will try make videos this week on the contest
@@codestorywithMIK will be waiting nd tried alot of videos but the intuition part is mission....
nice...
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
That's not a ghatia approach.
It's a common sense approach
@@331sachinkiragi8 Ya I tried same approach but not passed all the test cases 😢
@@331sachinkiragi8 Thanks bro for motivating
@@smile-zu2fg yeah, INT_MAX mein runtime error de diya, out_of_range
dry run me ek mistake hogaya hai 10^(n-1)+1 ....... 101 ki jagah 1001 likh diye aap galti se
Thank you ❤️
And apologies for silly mistake
yep. samajh gaya tha mai.
at what timestamp
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.
Correct hai i think . But koi na, mai samajh gaya
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
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.
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.
I
guess space complexity is O(N)
bhaiya please try to upload the video at morning
❤❤
har 2 min m ad kyu ara hai
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
Bro thoda jaldi daala karo na video
Sir please check you mail!
Thanks
what about numbers like 18 ,10 15 ,7,6 they wont follow your logic
Sach me
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.
18 ko karenge toh 11 ayega fir 22 kaise ayega
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.
why 999 ans is 1001 but smallest is 989
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.
@@priyaaofficial7 ya got it
@@priyaaofficial7 finally complete huaa
With brute force used long but only 163/217 test cases passed 🥲
Glad to see that you tried Brute Force ❤️
Bhai cp kru?
You tube to kar nhi paa raha CP kaha se karenga.
@@omkumarjha2745 apne channel pr dhyan de
@@Engineering.Wallah Nhi de paaya isliye chor diya
It means that these types of problems cannot be solved without knowing the testcase which is not possible to see during actual examination 🥲