BS-21. Median of two Sorted Arrays of Different Sizes | Binary Search Approach With Intuition

Sdílet
Vložit
  • čas přidán 20. 07. 2024
  • Problem Link: bit.ly/43QDw96
    Brute and Better: • BS 21: Median of two S...
    Notes/C++/Java/Python codes:
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course

Komentáře • 304

  • @abhik6400
    @abhik6400 Před 3 měsíci +78

    There is no way that you can come up with this optimal solution in an interview. Although the better solution using merge procedure from merge sort was pretty thinkable and doable but this is a completely genius solution !!!

    • @titusandronikus1337
      @titusandronikus1337 Před 3 měsíci +5

      I came up with it on my own when solving it on Leetcode. Let’s be honest, the main idea is not very hard. But my problem was the actual implementation. You can see in the video just how many random +1 and -1 we need, as well as boundary checks. It’s crazy. I hoped Striver would find a way to make the code less ugly - sadly, no. The problem is just inherently very annoying

    • @abhik6400
      @abhik6400 Před 3 měsíci +1

      @@titusandronikus1337 Really glad that you were able to come with the optimal solution on your own !!!!

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

      I came up with different approach on this one when solving on my own. It's similar to what we do in matrix's binary search I guess ( I have not watched striver's videos on it). Basically searching for kth element in any sorted arrays. It took O( log(m*n)*log(max-min)) time complexity, pretty big but it's in log and was accepted in leetcode.

    • @cosmicthor7330
      @cosmicthor7330 Před měsícem +2

      @@titusandronikus1337 same thought process is thinkable but seriously the implementation is though,hoestly i didnt understand fully

    • @rajat5040
      @rajat5040 Před 20 dny

      @abhik6400 can u tell how it is doable from merge sort???

  • @user-sv6hh4gt4z
    @user-sv6hh4gt4z Před 6 měsíci +35

    I am so dumb even after solving good number of questions on leetcode I even could not even think of like this.

  • @ravirajshelar250
    @ravirajshelar250 Před 10 měsíci +180

    The going into recursion for swapping idea was 🔥

    • @easylearn8924
      @easylearn8924 Před 8 měsíci

      can you explain why he does that?? or we also use min(n1,n2) but it gives runtime error why??

    • @tovenkatesh82
      @tovenkatesh82 Před 8 měsíci

      @@easylearn8924 the idea is to do a binary search over the smaller-size array. while loop is written based on that and that's why using min(n1,n2) would give you error.

    • @easylearn8924
      @easylearn8924 Před 8 měsíci

      ok thanks@@tovenkatesh82

    • @ashish4k07
      @ashish4k07 Před 8 měsíci

      @@easylearn8924 If we do that that's also possible but the code complexity will be too large and the std. while loop of binary search won't work even I understood after that video.

    • @easylearn8924
      @easylearn8924 Před 8 měsíci

      why it won't work in while loop can you explain?? because i able to understand but after sometime i confused in this part??@@ashish4k07

  • @sanketkumbhar8887
    @sanketkumbhar8887 Před 11 měsíci +97

    He has already explained this in sde sheet but still he made a video for a2z sheet💯

    • @farazahmed7
      @farazahmed7 Před 9 měsíci +5

      On which sheet has he explained this ? can you give me the link. Thanks

    • @mrlord8519
      @mrlord8519 Před 7 měsíci

      ​@@farazahmed7maybe from his sde sheet for placements

    • @omkarshendge5438
      @omkarshendge5438 Před 13 dny

      @@farazahmed7 i think he is talking about the placement series or the sde sheet of 180 questions he made long time ago, you should check that out.

  • @yatendraupadhyay2180
    @yatendraupadhyay2180 Před 3 měsíci +13

    Striver you are a real social reformer.
    At times when colleges are rendering students unemployable , you are making us industry ready.
    Dude Hats off to you.

  • @harshit.53
    @harshit.53 Před 3 měsíci +5

    If i hadn't checked this video there is no way i would be able to think of this solution in interview
    Thanks...

  • @shubhambagul3127
    @shubhambagul3127 Před 11 měsíci +23

    Waiting for this one for a long time no one explained this problem this well , Thank you.

  • @ruturajchandgude6083
    @ruturajchandgude6083 Před 10 měsíci +21

    Watched both videos twice ,all 3 approaches are crystal clear now,thank you!

  • @prajaktachachad477
    @prajaktachachad477 Před 4 měsíci +7

    I wanna know, how you built your logic and how you became an expert in understanding this logic so well. I have been following your playlist for a couple of months and understood each problem so well. What steps do you follow in your initial stage to reach this point? Please help so that your valuable tips can help me crack coding interviews. Trust me you are simply Amazing and Genius :)

  • @Dontpushyour_luck
    @Dontpushyour_luck Před 9 měsíci +26

    best video of entire playlist. I never understood this problem's binary search approach earlier, but you solved it so well. And that idea to call that function again if sizeof(b)

    • @user-cd7lf8nk4c
      @user-cd7lf8nk4c Před měsícem

      Why we need to do that ?
      Can you explain

    • @Beeplov2337568
      @Beeplov2337568 Před 19 dny

      @@user-cd7lf8nk4cIt might possible that the first array has greater size,so in order to take the shorter array to proceed he did it, hence TC : O(log(min(n1,n2)))

  • @mrsmurf911
    @mrsmurf911 Před 8 měsíci +12

    That swapping of the inputs and >>1 steps are 🔥 🔥

    • @ashish4k07
      @ashish4k07 Před 8 měsíci

      bit manupulation and swapping is to low so yeah it improves time mostly

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

    Such a thorough explanation! Exactly what I needed to help me understand this problem. Great energy throughout and the lesson was clearly well prepared and organized to educate and enlighten. Thank you!

  • @SwatiSingh-ys6hm
    @SwatiSingh-ys6hm Před 10 měsíci +7

    This is one of the bestest explanations I have come across. Totally cleared my concept. Thanks a lot sir !

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

    After watching so many videos i actully the found the gem which resolved my all the doubts in such a nice and simple way.

  • @utsavseth6573
    @utsavseth6573 Před 10 měsíci +7

    Understood. GOod video striver. It's important to watch these important questions because it is not possible to invent these kind of solutions then and there itself.

  • @rahulseetharaman4525
    @rahulseetharaman4525 Před 10 měsíci +3

    Crystal clear explanation. Explained your heart out. Thank you :)

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

    Best video explanation of this problem on the whole internet.

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

    Understood! Super amazing explanation as always thank you very very much for your effort!!

  • @pranavindore2410
    @pranavindore2410 Před 9 měsíci +1

    TOP notch explanation striver. I saw both videos. Understood completerly. Thank you.

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

    Once again, your explanation is top-notch.

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

    What a energy !
    Thank you striver for amazing content 🙇

  • @harshhwardhanrai3716
    @harshhwardhanrai3716 Před 24 dny +1

    This is the first video that I have not understood of you. No matter how many times I watch I just can't understand. I'm just skipping this optimal approach for now. :)

  • @linhnguyenduc641
    @linhnguyenduc641 Před 3 měsíci

    You deeply understand the problem and explain the solution well. Thanks.

  • @sibashis_12
    @sibashis_12 Před 8 měsíci +1

    brilliant explanation. even hard topics seem easy when you explain them.

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

    brilliant explanation, this problem is not only hard to do but also hard to explain

  • @aruna5869
    @aruna5869 Před 25 dny

    I shocked at the end of video after seeing the way you explained this complex optimal solution!!!!
    Thanks a lot!❤‍🔥💥❤💯

  • @Anshydv3
    @Anshydv3 Před 10 měsíci +7

    The king of coding community 👑

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

    UNDERSTOOD..........Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @Health_asset
    @Health_asset Před měsícem +1

    Thanks striver to explain this . I was thinking that this is too much difficult concept but after watching this video , I can do the similar stuff myself. Thank you so much

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

    Amazing, how you observe so minutely :) Bhai Hat's Off .

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

    I did it using the approach of two sorted lists question and got 2ms solution. But this is better

  • @shshnk11
    @shshnk11 Před 10 měsíci +3

    Brilliantly explained!!

  • @VikasSharma-eg8mc
    @VikasSharma-eg8mc Před 11 měsíci +2

    Understood!! Amazing explanation

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

    Really wonderful approach and explanation

  • @yasaswinikarumuri9590
    @yasaswinikarumuri9590 Před 24 dny

    I still can't imagine how would someone think of such an optimal solution? It's out of mind. Are we expected to think of such optimal soln? I'm asking this bcz, it took me lot of time to understand this soln even after a great explanation...
    Thank you striver for such a wonderful explanation !

  • @ashwingoel7173
    @ashwingoel7173 Před 5 měsíci +6

    At 17:57 shouldn't it be l1 > r2?

  • @pratulyapratap9329
    @pratulyapratap9329 Před 5 měsíci +1

    Maybe it will help :)
    int mid2 = left - mid1; // left = how many elements i can pickup mid1 = how many i have picked up

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

    Great explaination..Thank you.💯

  • @user-kc4yv5kt3j
    @user-kc4yv5kt3j Před 10 měsíci

    Wow explanations. Big Thanks to Striver.

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

    1 morning i would woke up and see striver had completed a2z series and i got my dream company.

  • @jaypratap3888
    @jaypratap3888 Před 2 měsíci

    Thanks Bhai. Its a tough question, but explained it very nicely.

  • @BookJournalWithMimansa
    @BookJournalWithMimansa Před měsícem

    Amazing Explanation! Thanks!

  • @sayyidzyanashraf
    @sayyidzyanashraf Před měsícem +1

    Hats off to you Broh... THANKS A MILLION 💙💙💙

  • @welcometoc.s.easpirants
    @welcometoc.s.easpirants Před 4 měsíci

    Great explanation. Thank you ❤

  • @joeljacob4685
    @joeljacob4685 Před 8 měsíci +1

    Excellent explanation!!💌

  • @mano_003
    @mano_003 Před 11 měsíci +8

    Thank u for doing things for us even in ur busy days...❤

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

      "Busy" are those People who disrespect others, People who respect are not Busy ❤

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

    At first the brain wasn't braining but got it at the end great explanation

  • @nandini62
    @nandini62 Před 4 dny

    Thank you so much broo for these series ☺️

  • @atulanand6428
    @atulanand6428 Před měsícem

    I have also solved this but using another method:
    Approach was to iterate one smaller array from 1 to n and applying binary search and insert the element into another vector using bs.

  • @sauravsarkar4785
    @sauravsarkar4785 Před 5 měsíci

    great explanation buddy. Keep up the good work.

  • @user-jg9vc7mf1e
    @user-jg9vc7mf1e Před 9 dny

    your explanation is awesome 😇😇. Finally i can rest in peace🙃

  • @maneeshkumarpatel9874
    @maneeshkumarpatel9874 Před 20 dny

    Loved this approach❤

  • @javabytharun
    @javabytharun Před 2 dny +1

    awesome explanation

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

    Thank you Striver sir 🥰

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

    Thank you Striver💖💖

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz Před 5 měsíci

    before watching this intution , my favourite intution was dutch national flag algo,,, but this question along with its explanation was beyond my imagination,,,, hats off to you.......and your expression after completing this ques shows how passionate you are about your work and this gives us too much motivation,,,thank you😇😇

    • @arjunc1482
      @arjunc1482 Před 5 měsíci

      bro..how will you use dutch national flag algo for this question?

    • @ArpanChakraborty-do6yz
      @ArpanChakraborty-do6yz Před 5 měsíci

      @@arjunc1482 I am not saying I will use duch algo here,,, I have just stated among all algo/intuitions duch algo and it's question was my fav,,, but after watching this question and it's soln , it is my fav now

  • @empvaibhav9799
    @empvaibhav9799 Před měsícem +1

    The idea clicked the moment he said how many elemts to pick from both the arrays.
    Started coding it and damn it was tough to code it (edge cases 💀)

  • @BhaweshKumawat
    @BhaweshKumawat Před 5 měsíci +1

    17:55 l1 > r2

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

    great video, thanks for this

  • @souvikcseiitk
    @souvikcseiitk Před 10 dny

    this tutorial is awesome, thanks for this :)

  • @charuprabha9696
    @charuprabha9696 Před měsícem +1

    This is a little bit too much for me to digest, but at least I understood most of it.🙂

  • @nishaaa_maurya
    @nishaaa_maurya Před 4 měsíci

    Hi Striver, It was a great explanation. Thank you !!
    Can you please explain that, why are taking first vector is always smaller?

  • @ShubhamKumar-uf3gc
    @ShubhamKumar-uf3gc Před 13 dny

    CLEAN AS ALWAYS

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

    very well explained!!!

  • @ayushhagarwal
    @ayushhagarwal Před 25 dny

    Understood! Thanks!!

  • @ReD4eva94
    @ReD4eva94 Před 4 měsíci

    Brilliant. Thanks!

  • @GoodLuck-dv2zu
    @GoodLuck-dv2zu Před 3 měsíci

    I think the time complexity is not the only reason why you should do a binary search on an array whose size is smaller. If you will do a binary search on the array with a bigger size, then you will not be able to construct the first array (left partition) to make two arrays asymmetrical

  • @rajnandinipatel81
    @rajnandinipatel81 Před 6 měsíci

    brilliant explanation

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 3 měsíci

    Understood, thank you.

  • @ankitsharda1131
    @ankitsharda1131 Před 7 měsíci

    Best Explanation!!

  • @SHUBHAMSINGH-nv7ot
    @SHUBHAMSINGH-nv7ot Před 3 měsíci +1

    17:52 l1 is greater than r2 (correction)

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

    Awesome !👍

  • @lalitbisht8381
    @lalitbisht8381 Před 3 měsíci

    Mind blowing video❤

  • @RGUKTEDUIN
    @RGUKTEDUIN Před 5 měsíci

    Understood salute to striver🤓

  • @shivanibharti4774
    @shivanibharti4774 Před 7 měsíci

    Great.. You are the best

  • @PrashantSingh.
    @PrashantSingh. Před 6 měsíci

    hats of to your efforts

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

    great explanation

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

    Finally Understood man.

  • @DhananjayKumar-bd2jg
    @DhananjayKumar-bd2jg Před 6 měsíci +1

    why can't we take this case to eliminate right?
    if(l1 > r2 || l2 > r1) high = mid - 1;

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

    Understood bhaiya 😊

  • @kale-lb5pr
    @kale-lb5pr Před 6 měsíci

    i think so to find out left we can use (n1+n2)/2 and change median as min(r1,r2) nothing much difference right??

  • @drishtirai864
    @drishtirai864 Před 10 dny

    Thank you, Sir ! :)

  • @KESHAVKUMAR-mb2nm
    @KESHAVKUMAR-mb2nm Před 10 měsíci

    Understood, Thank U

  • @harshitjaiswal9439
    @harshitjaiswal9439 Před 6 měsíci

    Loved it!

  • @we_atheletes
    @we_atheletes Před 8 měsíci +1

    one of best video on yt

  • @sagark4080
    @sagark4080 Před 8 měsíci +3

    @takeUforward, @30.01 generally in Binary search of array we consider left =0 and right=array.size()-1 correct?
    But here why have you considered low =0 and high=n1 ( which is array size itself) not n1-1?

    • @takeUforward
      @takeUforward  Před 8 měsíci +1

      Yes because it means how many elements we take, either we can take 0 elements or we can take all which is n1

    • @Donquixote-Rosinante
      @Donquixote-Rosinante Před 7 měsíci

      i tried to do it in zero-based. if e.g nums1=[1,3] nums2 = [2].
      arr1Left = 2,
      arr1Right = inf,
      arr2Left = -inf,
      arr2Right = 1.
      arr1Left

  • @ManavMSanger
    @ManavMSanger Před 7 měsíci

    understood. Thanks

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

    very helpful thanks bhaiya

  • @abhaythakur2597
    @abhaythakur2597 Před 5 měsíci

    really well understood

  • @snehachauhan237
    @snehachauhan237 Před 20 dny

    it was superb................

  • @arkadiptamojumder3800
    @arkadiptamojumder3800 Před 9 měsíci +5

    l1 should be greater than r2 right at 17:49 ?

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

    Understood!!!!

  • @blue.262
    @blue.262 Před 11 měsíci

    Thank you striver sir

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

    thankyou. this is best

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

    over the top bhaiya

  • @tanushree0106
    @tanushree0106 Před 2 měsíci

    Thank you so much sir

  • @shaiksoofi3741
    @shaiksoofi3741 Před měsícem

    Thank you

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

    Understood ❤

  • @hashcodez757
    @hashcodez757 Před 5 měsíci

    Understood sir!!

  • @YourCodeVerse
    @YourCodeVerse Před 6 měsíci

    Understood✅🔥🔥