Minimize the Maximum Difference of Pairs | Binary Search Pattern | INTUITION | META | Leetcode-2616

Sdílet
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

Komentáře • 105

  • @sachinmohanty4577
    @sachinmohanty4577 Před 11 měsíci +16

    You are not shaping the thought process MIK you are shaping someone's hope and aspirations to be a great problem solver❤

  • @champhero4815
    @champhero4815 Před 2 měsíci +2

    what an explnation , truley amazing

  • @mithungupta3726
    @mithungupta3726 Před 11 měsíci +4

    1 min pahele hi khud se solve kr liya but , keep uploading the video everyday

  • @Thriftinghai
    @Thriftinghai Před 11 měsíci +2

    Such a detailed explanation. One of the finest youtubers you are

  • @herculean6748
    @herculean6748 Před 11 měsíci +1

    intuition is explained very well !!

  • @AlishaKhan-ww3io
    @AlishaKhan-ww3io Před 11 měsíci +1

    Masterpiece explanation as always. You are awesome

  • @amarnathgautam478
    @amarnathgautam478 Před 11 měsíci +1

    building the thought process part of your videos are gold

  • @mohithadiyal6083
    @mohithadiyal6083 Před 11 měsíci +1

    Great explanation

  • @user-kw2bg6wt1v
    @user-kw2bg6wt1v Před 11 měsíci +2

    what an explanation thats too free of cost. thank you so much 😊

  • @crashcartjoe6783
    @crashcartjoe6783 Před 11 měsíci +2

    Maza aa gya step by step explanation

  • @anuppatankar4294
    @anuppatankar4294 Před 11 měsíci +2

    I coded it myself after watching your explanation 👌🏻

  • @ShubhamPandey-mi4xt
    @ShubhamPandey-mi4xt Před 11 měsíci +1

    Your explanations are so clear and intuitive! Thanks for these videos!

  • @souravjoshi2293
    @souravjoshi2293 Před 11 měsíci +1

    was waiting eagerly for your video

  • @honey6567
    @honey6567 Před 11 měsíci +2

    youtube me sab bewakoof bana rahe sabko bs like ke liye.. you are greate mentor in future

  • @umeshbisht1054
    @umeshbisht1054 Před 11 měsíci +1

    Thanku bhaiya ❤

  • @tutuimam3381
    @tutuimam3381 Před 11 měsíci +2

    Thanks a lot

  • @honey6567
    @honey6567 Před 11 měsíci +2

    you are great sir please launch you course for student

  • @divyanshsagar
    @divyanshsagar Před 11 měsíci +1

    Awesomely explained!

  • @souvikmukherjee8245
    @souvikmukherjee8245 Před 11 měsíci +1

    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.

    • @elakstein
      @elakstein Před 11 měsíci +2

      Dry run karo step by step for each function call, write down the parameter passed during each call, mehnat lagegi par samjh aayegi.

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +1

      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 😇🙏❤️

    • @souvikmukherjee8245
      @souvikmukherjee8245 Před 11 měsíci

      @@codestorywithMIK thank u ❤️

  • @SFsachin
    @SFsachin Před 11 měsíci +2

    You're improving people's life bro.

  • @alphadrones23
    @alphadrones23 Před 11 měsíci +1

    amazing thought process....

  • @mewaconite
    @mewaconite Před 11 měsíci +1

    binary search on answer... good explanation, like always keep it up :)

  • @honey6567
    @honey6567 Před 11 měsíci +4

    sir codeforce bhi start kar do please

  • @sachinmohanty4577
    @sachinmohanty4577 Před 11 měsíci +2

    Lovely 🎉🎉

  • @ugcwithaddi
    @ugcwithaddi Před 11 měsíci

    Masterpiece ❤

  • @Jaipurhunters
    @Jaipurhunters Před 11 měsíci +1

    good work bro your videos help alot keep me motivated

  • @neelx9437
    @neelx9437 Před 11 měsíci +1

    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]

  • @beinghappy9223
    @beinghappy9223 Před 11 měsíci +1

    Thanks for the amazing explanation

  • @pratyushpandey1145
    @pratyushpandey1145 Před 9 měsíci

    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

  • @bhartipatel7833
    @bhartipatel7833 Před 11 měsíci +3

    me(before watching your video) : binary search nhi banta hard h
    me(after watching your video) : are binary search to bht easy h
    🙂

  • @souravjoshi2293
    @souravjoshi2293 Před 11 měsíci

    Nailed it bro

  • @sauravchandra10
    @sauravchandra10 Před 11 měsíci +1

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

  • @none2868
    @none2868 Před 11 měsíci +1

    Best explanation

  • @mailatkaushal
    @mailatkaushal Před 10 měsíci +1

    best explanation...

  • @advaitnene3735
    @advaitnene3735 Před 11 měsíci +1

    Great explanation as always! Thank you for these videos!
    Can anyone share the DP solution with an explanation if possible?

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci

      I will share them in DP playlist separately 😇🙏

  • @dhairyachauhan6622
    @dhairyachauhan6622 Před 11 měsíci +1

    this was actually hard to think...definately going in my excel sheet :)

  • @aman3210
    @aman3210 Před 11 měsíci +1

    THANKS MK BHAIYA + 8 ;

  • @YashSinghal
    @YashSinghal Před 11 měsíci +1

    after 10:33 i coded it all by myself i remembered what you taught in earlier videos. Thanks !!

    • @YashSinghal
      @YashSinghal Před 11 měsíci

      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

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +1

      I am so glad.
      Thank you so much 😇🙏

  • @xiaoshen194
    @xiaoshen194 Před 11 měsíci +2

    Sir plz cover Rank transform of a matrix on leetcode. Aapne promise kiya tha ki graph concept playlist mein cover krenge

  • @codeandtalk6
    @codeandtalk6 Před 11 měsíci +1

    ❤❤

  • @diveshsaini
    @diveshsaini Před 11 měsíci +1

    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.

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +1

      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

  • @taneyasoni
    @taneyasoni Před 11 měsíci +1

    #augustchllangewithMIK lovely explanation.

  • @Rajat_maurya
    @Rajat_maurya Před 11 měsíci

    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 ?

  • @user-jq3yi5go6u
    @user-jq3yi5go6u Před 11 měsíci +1

    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.

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +1

      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

    • @user-jq3yi5go6u
      @user-jq3yi5go6u Před 11 měsíci +1

      @@codestorywithMIK thanks , because of this confusion even though i knew its was a binary search question i had a hard time applying it .

  • @doomhead9109
    @doomhead9109 Před 11 měsíci

    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

  • @princejatav8456
    @princejatav8456 Před 11 měsíci

    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

  • @abishekbaiju1705
    @abishekbaiju1705 Před 10 měsíci

    I have trouble understanding hindi. I am seeing very positive responses on the comments can yo do these videos in english?

  • @darshandesale
    @darshandesale Před 11 měsíci

    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

  • @user-cx4tf7fg6f
    @user-cx4tf7fg6f Před 11 měsíci +1

    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

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 11 měsíci

    Class solution {
    Public int minimize Max(int[]nums,int p){
    Array sort(nums);
    int left =0 right =[nums. length-1]-nums [0];
    While (left

  • @shauncrasta619
    @shauncrasta619 Před 11 měsíci +1

    I had a doubt of why not checking if the difference is equal to the threshold/mid. I wanted a more detail proof why

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci

      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)

    • @shauncrasta619
      @shauncrasta619 Před 11 měsíci +1

      @@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?

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +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

    • @shauncrasta619
      @shauncrasta619 Před 11 měsíci +1

      @@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! ❤️

    • @elakstein
      @elakstein Před 11 měsíci

      @@codestorywithMIK what if both (b-a and c-d) is

  • @daayush6654
    @daayush6654 Před 11 měsíci +4

    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

    • @RahulGuptaa29
      @RahulGuptaa29 Před 11 měsíci +1

      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

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +2

      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.

    • @daayush6654
      @daayush6654 Před 11 měsíci +1

      @@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

    • @codestorywithMIK
      @codestorywithMIK  Před 11 měsíci +2

      I am glad you are planning for internships.
      That’s going to help you a lot.
      All the very best

    • @wearevacationuncoverers
      @wearevacationuncoverers Před 11 měsíci +1

      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