Minimize the Maximum Difference of Pairs | Binary Search Pattern | INTUITION | META | Leetcode-2616
Vložit
- čas přidán 27. 07. 2024
- Similar Problem -
Minimize Maximum of Array : • Minimize Maximum of Ar...
Koko Eating Bananas - • Koko Eating Bananas - ...
Maximum Value at a Given Index in a Bounded Array - • Maximum Value at a Giv...
Hi everyone, this is the 21st video of our Binary Search Playlist.
In this video we will try to solve a very famous pattern of Binary Search problem “Minimize the Maximum Difference of Pairs” (Leetcode-2616).
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Minimize the Maximum Difference of Pairs
Company Tags : META
My solutions on Github : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/minimiz...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Introduction and Problem understanding - 00:00
Explanation starts - 4:21
Brute Force - 6:16
Further optimization and intuition - 6:41
Full dry run - 19:47
Story to code - 26:51
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#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
You are not shaping the thought process MIK you are shaping someone's hope and aspirations to be a great problem solver❤
Thank you so much Sachin ❤️❤️
Kya hai sachin mai......😄
totally agree
indeed
this guy is leaving a mark which no one has ever done on youtube. I have never seen such finest explanations before
what an explnation , truley amazing
1 min pahele hi khud se solve kr liya but , keep uploading the video everyday
Awesome ❤😊
Such a detailed explanation. One of the finest youtubers you are
intuition is explained very well !!
Thank you 😇🙏
Masterpiece explanation as always. You are awesome
building the thought process part of your videos are gold
Means a lot ❤️😇🙏
Great explanation
what an explanation thats too free of cost. thank you so much 😊
Thank you so much 😇🙏
Maza aa gya step by step explanation
Thank you so much 😀
I coded it myself after watching your explanation 👌🏻
Awesome 😇🙏
Your explanations are so clear and intuitive! Thanks for these videos!
Thanks a lot 😇🙏
was waiting eagerly for your video
youtube me sab bewakoof bana rahe sabko bs like ke liye.. you are greate mentor in future
Thank you 😇🙏
Means a lot
Thanku bhaiya ❤
Thanks a lot
you are great sir please launch you course for student
Awesomely explained!
Thanks a lot 😇
Finally, someone explained the problem clearly. Pura samajh me aa gaya ab. Recursion k code me for loop samajh ne main dikkat aa raha hai bohot mujhe. kya next recursion k code k video main ap thoda explain kar denge when to use for loop and what u are thinking while writing a for loop inside a recursive function.
Dry run karo step by step for each function call, write down the parameter passed during each call, mehnat lagegi par samjh aayegi.
Actually, tree diagram is my only friend which helps me figure out that. Doing a dry run indeed helps a lot.
But in next recursion problem, i will definitely put more stress on that.
Thank you so much 😇🙏❤️
@@codestorywithMIK thank u ❤️
You're improving people's life bro.
Means a lot 😇🙏
amazing thought process....
😇🙏
binary search on answer... good explanation, like always keep it up :)
Thank you 😇🙏
sir codeforce bhi start kar do please
Will soon plan ❤️
Lovely 🎉🎉
Masterpiece ❤
good work bro your videos help alot keep me motivated
Thank you 😊
was failing at 1119/1582 test cases. then saw ur video . Corrected the logic of valid function. Thanks again❤
Here's my Java code-
class Solution {
private boolean valid(int[] nums, int p, int mid) {
int i = 0, count = 0;
while (i < nums.length - 1) {
if (nums[i + 1] - nums[i]
Thanks for the amazing explanation
So glad it helped 🙏😇
Sir the question of leetcode MAximise the tastiness of candy was quite similar to this one. i was able to solve half logic but had to go to look for ans for full solution. Thanks again as always
me(before watching your video) : binary search nhi banta hard h
me(after watching your video) : are binary search to bht easy h
🙂
Thank you 😇❤️
Nailed it bro
Before starting this video, I would like to mention that this really indeed is an easy problem and I was able to code this on my own once I got to know this could be solved using BS. There are few days when I am able to solve these problems without seeing your approach and these days prove to me that I am going in the right path. Now to bhaiya's approach....
Awesome ❤️😇
Best explanation
Thank you so much 😇🙏
best explanation...
Thank you for watching 😇🙏
Great explanation as always! Thank you for these videos!
Can anyone share the DP solution with an explanation if possible?
I will share them in DP playlist separately 😇🙏
this was actually hard to think...definately going in my excel sheet :)
Same 😇🙏
THANKS MK BHAIYA + 8 ;
😇❤️
after 10:33 i coded it all by myself i remembered what you taught in earlier videos. Thanks !!
I checked my code with yours at the end. I have started writing code almost the way you write haha. My code: bool possible(vector& nums, int p, int m){
int i = 1;
int count = 0;
while(i
I am so glad.
Thank you so much 😇🙏
Sir plz cover Rank transform of a matrix on leetcode. Aapne promise kiya tha ki graph concept playlist mein cover krenge
Noted 👍🏻
@@codestorywithMIK thnx
❤❤
bhai 1-2 ghnte problem solve nhi hoti tb video dekhni pdti h or ate hi aap bolte ho medium marked hai but actually very easy h, this hits me very badly.
Hi Divesh, please don’t feel so. Actually there are two reasons i say so :
1) To remove the fear in the beginning.
2) To make you believe that by the end of my video, it will seem an easy problem if intuition is clear
Hope this helps. Do not worry, in the beginning for many days, we stumble, and then the growth is exponential with time
#augustchllangewithMIK lovely explanation.
Thank you 🙏 😇
What do you think of CP ? I am not able to solve the codeforces div 2 problem C, do i need to work on it or just leetcode solving and giving contest can land me a good job ?
can u tell the logic behind choosing adjacent elements because isn't it possible that their could be a pair giving the required difference but are not adjacent.
This example below is taken from internet but I think this is a very good explanation, hence sharing this here --
Imagine you have [a,b,c,d], abcd are just sorted numbers
Greedy tells us if b - a < the threshold we set, we just pick it. The cost of doing this is if c - b < threshold as well, you can never pick bc pair anymore because b has been paired with a.
Will this cause any problems?
The answer is no
Because if you pick ab pair, you will have one pair now. If you pick bc pair, you also have one pair, however, if you pick bc now what's left is [a, d]. Because we have sorted abcd, d - a is likely to be bigger so it is more likely to exceed the threshold we set.
On the other hand, if we greedily pick ab pair, what's left is [c,d] and d - c is guaranteed to be smaller or equal to d - a, so d-c is more likely to be a pair that we can pick as well -> being greedy (pick a and b) is more likely to let us have more pairs then not being greedy (pick b and c). Hence for this problem, greedy works
@@codestorywithMIK thanks , because of this confusion even though i knew its was a binary search question i had a hard time applying it .
sir I don't know why but I am facing a issue usually I can write recursive call easily with a global variable but most of time I was not able to write a recursive call which return a result of it's recursive calls
Bro i have doubt u have taken MID suppose if on the first iteration (MID = L+(R-L)/2) there are pairs less than (P), so then we will move to higher values of ANS which is (ANS>MID) or we will put (L) to (MID+1) ,,,,but how can you assure that if we dont find ans for initial mid value, then it will not be less than mid...
i hope u understood my problem, plzzz reply
I have trouble understanding hindi. I am seeing very positive responses on the comments can yo do these videos in english?
sir please muze ye batao ki me jab gaya leetcode pe iss wale qna pe to dar gaya or muze pehle to samjha hi nahi qna but pata tha bs ka he
or uske bad me apke video pe only qna samzh ne ke liye aya or mene apne app hi code or logic or edeg cases bhi find kiye
solution bata sir iss meri problemka kaha kami he namak me
JAVA CODE :
class Solution {
public int minimizeMax(int[] nums, int p) {
Arrays.sort(nums);
int low= 0;
int high = nums[nums.length-1]-nums[0];
int result = Integer.MAX_VALUE;
while(low
Class solution {
Public int minimize Max(int[]nums,int p){
Array sort(nums);
int left =0 right =[nums. length-1]-nums [0];
While (left
I had a doubt of why not checking if the difference is equal to the threshold/mid. I wanted a more detail proof why
Good Qn.
Take this example :
[10,1,2,7,1,3]
p = 2
This is how it will go if we only check for == mid;
For mid = 4, I Got = 1 countPairs (But we need p = 2 pairs)
For mid = 7, I Got = 0 countPairs (But we need p = 2 pairs)
For mid = 8, I Got = 0 countPairs (But we need p = 2 pairs)
For mid = 9, I Got = 0 countPairs (But we need p = 2 pairs)
So, at the end we won't find any result.
Also, if you take it like this, for p pairs -
(a, b) , (c,d) (let p = 2)
If your result is 1, it means that max of (b-a, c-d) must be 1
And, this means that any one of (b-a)
@@codestorywithMIK thank you for taking the time to explain it..but shuldnt either of b-a or c-d must be equal to 1 for max to give a result as 1?
Yes definitely. If anyone of them gives 1, then max will be 1.
But if we have p pairs, then some of the pairs we choose might have less than 1 as difference but since we pick max of all differences, we will get 1.
So in order to count those pairs we need
@@codestorywithMIK Yes got it, as the motive here maximise the search space for pairs. I really crammed ChatGPT for some explanations for it. Btw a good video. Really appreciate your work! ❤️
@@codestorywithMIK what if both (b-a and c-d) is
Bhaiya jitni bhi offcampus companies ki opportunity aai usme sabko web dev cahiye, DSA skills check bhi nai kar rahe.
Idhar web aata nai hai aur college ko ai/ml ke paper publish karvane he, woh bhi acche se nai aata.
College is just so irritating, ab kuch padha ho toh bhi ,esa kaam de dete hai Jo aata bhi na ho aur karne ke liye zyada time cahiye aur deadline 1 week ki de dete hai, aur ese 4-5 kaam de dete hai, kuch padha ho toh bhi man nai karta ye college ki vajah se, aapke hisab se kya karna cahiye
bhai dsa pe dhyaan do ai/ml chatgpt se nikal ke chep do or else its your loss main. Web dev ka pata nahi yaar main bhi same situation main hu
DSA is something which should always be studied. It will be always with you wherever you work.
But today due to increased competition, we have to be good in development as well.
Those who are good in dsa+development will get preference.
Need to make out time to work on both.
For freshers, better to do the mini and major project very well and mention in the resume.
@@codestorywithMIK mini project bhi ai/ml ka hai bhaiya, major project toh mae karunga hi nahi 6 months internship hi opt kar lunga kyuki usme option hai. (If 7th sem mae pass ho Gaye toh)
Web ka wahi se training le lunga (ek service company hai), but DSA accha lagta hai, abhi consistency nahi todi thanks to you
I am glad you are planning for internships.
That’s going to help you a lot.
All the very best
I think one must not skip DSA whatever happens. I recently gave interview for Cisco and I was asked 2 Qns on Dp which I solved using recursion memoization. Those who couldn't solve were not moved to further rounds