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

Поділитися
Вставка
  • Опубліковано 28 лис 2024

КОМЕНТАРІ • 409

  • @abhik6400
    @abhik6400 8 місяців тому +196

    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 7 місяців тому +19

      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 7 місяців тому +1

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

    • @priyanshurana6027
      @priyanshurana6027 6 місяців тому +5

      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 5 місяців тому +2

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

    • @rajat5040
      @rajat5040 5 місяців тому +1

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

  • @yatendraupadhyay2180
    @yatendraupadhyay2180 7 місяців тому +34

    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.

  • @ravirajshelar250
    @ravirajshelar250 Рік тому +267

    The going into recursion for swapping idea was 🔥

    • @easylearn8924
      @easylearn8924 Рік тому +1

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

    • @tovenkatesh82
      @tovenkatesh82 Рік тому +2

      @@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 Рік тому

      ok thanks@@tovenkatesh82

    • @ashish4k07
      @ashish4k07 Рік тому +1

      @@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 Рік тому

      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 Рік тому +129

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

    • @farazahmed7
      @farazahmed7 Рік тому +5

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

    • @mrlord8519
      @mrlord8519 Рік тому

      ​@@farazahmed7maybe from his sde sheet for placements

    • @omkarshendge5438
      @omkarshendge5438 4 місяці тому

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

  • @prajaktachachad477
    @prajaktachachad477 9 місяців тому +17

    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 Рік тому +47

    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)

    • @VivekKumar-p2g4l
      @VivekKumar-p2g4l 5 місяців тому

      Why we need to do that ?
      Can you explain

    • @Beeplov2337568
      @Beeplov2337568 5 місяців тому

      @@VivekKumar-p2g4lIt 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)))

    • @beast2811
      @beast2811 Місяць тому

      @@VivekKumar-p2g4l he is taking the first array to be smaller

  • @ruturajchandgude6083
    @ruturajchandgude6083 Рік тому +28

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

  • @mrsmurf911
    @mrsmurf911 Рік тому +24

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

    • @ashish4k07
      @ashish4k07 Рік тому

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

    • @Manas-jj6xf
      @Manas-jj6xf 28 днів тому

      @@ashish4k07 Can you please explain why did he do bit manipulation there for find mid1?
      I've watched almost all his previous videos up until this problem, and he hasn't explained bit manipulation yet.

    • @ashish4k07
      @ashish4k07 28 днів тому +1

      @@Manas-jj6xf basically >> means right shift which divides the number

  • @shubhambagul3127
    @shubhambagul3127 Рік тому +26

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

  • @harshit.53
    @harshit.53 7 місяців тому +11

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

  • @harshhwardhanrai3716
    @harshhwardhanrai3716 5 місяців тому +25

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

  • @SwatiSingh-ys6hm
    @SwatiSingh-ys6hm Рік тому +10

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

  • @vartikasinghania9723
    @vartikasinghania9723 26 днів тому +1

    at 1st I thought i wouldn't understand this, but it stick till the end and it was so clear. Amazing explanation and it so motivating to see the efforts made explaining this. Thanks a lot!!

  • @TusharKumar-u4p
    @TusharKumar-u4p 10 місяців тому +74

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

    • @ShubhamKumar-l2u2x
      @ShubhamKumar-l2u2x 3 місяці тому

      same same

    • @arpitgoyal2035
      @arpitgoyal2035 3 місяці тому +4

      you just have to understand all the concept better and try to practice more problems on your own and like solve some old problems as well to strength your concepts and get motivation.

    • @dp622
      @dp622 3 місяці тому +18

      these problems are standard problems aka basic ones, no one can come up with solutions of such problems unless they are god gifted in mathematics
      The problems in contest are variation of such basic problem which can be solved by all if they know basic problems.

    • @Vishal-s2g7c
      @Vishal-s2g7c 3 місяці тому +3

      bro you get better by uderstand

    • @karunanikam3259
      @karunanikam3259 5 днів тому

      Same🥲

  • @Anshydv3
    @Anshydv3 Рік тому +7

    The king of coding community 👑

  • @utsavseth6573
    @utsavseth6573 Рік тому +8

    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.

  • @RaginiGupta-q1v
    @RaginiGupta-q1v 6 днів тому

    I don’t know whether you’ll read this comment or not, but believe me bhayia, I can’t imagine how I would have studied DSA without you to teach. Thank you so much, always. ♥

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 3 місяці тому +1

    Understood completely . Great man , you are God in DSA.

  • @shikhirkalia6828
    @shikhirkalia6828 11 місяців тому +2

    Best video explanation of this problem on the whole internet.

  • @Josuke217
    @Josuke217 4 місяці тому +2

    This is a great approach, no way I could come up with this in an interview...

  • @Dipanshutripathi2407
    @Dipanshutripathi2407 Рік тому +4

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

  • @yasaswinikarumuri9590
    @yasaswinikarumuri9590 5 місяців тому +2

    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 !

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 9 місяців тому +1

    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 9 місяців тому

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

    • @ArpanChakraborty-do6yz
      @ArpanChakraborty-do6yz 9 місяців тому

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

  • @aruna5869
    @aruna5869 5 місяців тому

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

  • @linhnguyenduc641
    @linhnguyenduc641 8 місяців тому

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

  • @ArunsinghParihar-j3j
    @ArunsinghParihar-j3j 3 місяці тому +1

    Mind Blowing Solution approach

  • @JeffreyConcerto
    @JeffreyConcerto Рік тому +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!

  • @abhicasm9237
    @abhicasm9237 Рік тому +2

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

  • @dhananjayadhari6481
    @dhananjayadhari6481 9 місяців тому +2

    Really wonderful approach and explanation

  • @sahilbani7020
    @sahilbani7020 Рік тому +1

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

  • @giteshkhanna2633
    @giteshkhanna2633 27 днів тому

    One case that could have been discussed:
    That is: Can there be a case where, l1 > r2 & l2 > r1. What to do in that case?
    Answer: There cannot be such a case. This can be proved.
    So let's assume l1 > r2 & l2 > r1 & try to reach to a contradictory statement.
    So we know that r1 > l1 (Because array is sorted)
    Therefore, r2

  • @rahulseetharaman4525
    @rahulseetharaman4525 Рік тому +3

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

  • @t3ch_r4id
    @t3ch_r4id 3 місяці тому

    Thanku for making optimal vedio separately for this problem 🥲❣️🙌🏻

  • @Health_asset
    @Health_asset 5 місяців тому +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

  • @shashankarora2945
    @shashankarora2945 3 місяці тому

    Explained so smoothly🔥🔥

  • @avinashkumar3116
    @avinashkumar3116 3 місяці тому +5

    If striver explains this question to a dead body and later asks him whether you understood or not. That dead body will yell from it's grave - "Understood Sir 💀☠"

  • @dxvya23
    @dxvya23 Рік тому +1

    Once again, your explanation is top-notch.

  • @ekanshsharma1309
    @ekanshsharma1309 24 дні тому

    God Level Explaination sir 🔥

  • @sibashis_12
    @sibashis_12 Рік тому +1

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

  • @atulanand6428
    @atulanand6428 5 місяців тому +1

    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.

  • @mano_003
    @mano_003 Рік тому +8

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

    • @Sports590
      @Sports590 Рік тому +3

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

  • @pranavindore2410
    @pranavindore2410 Рік тому +1

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

  • @playwithlinux
    @playwithlinux 5 місяців тому +1

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

  • @9-1939
    @9-1939 2 місяці тому

    Best video. I watched twice and understood great effort and awesome solution 🔥🔥

  • @technicalworld6470
    @technicalworld6470 Рік тому +2

    What a energy !
    Thank you striver for amazing content 🙇

  • @amitranjan6998
    @amitranjan6998 Рік тому +2

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

  • @shshnk11
    @shshnk11 Рік тому +3

    Brilliantly explained!!

  • @stith_pragya
    @stith_pragya 9 місяців тому +2

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

  • @cinime
    @cinime Рік тому +3

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

  • @pratulyapratap9329
    @pratulyapratap9329 9 місяців тому +1

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

  • @MrGohel-ni2py
    @MrGohel-ni2py 12 днів тому

    It is a Great question and explaination is just fantastic.

  • @yygysgtyfugunvt
    @yygysgtyfugunvt Місяць тому

    This man is genius

  • @martinropke3390
    @martinropke3390 23 дні тому

    Very good explanation, thank you for this video.

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

    Thank you striver for the amazing content 🙇

  • @bgovindnaren7405
    @bgovindnaren7405 2 місяці тому

    thanks bro
    kya gajab video tha...
    smaj aa gaya...

  • @we_atheletes
    @we_atheletes Рік тому +1

    one of best video on yt

  • @rushidesai2836
    @rushidesai2836 Місяць тому

    This was difficult to explain but great job Raj.

  • @sumitkamble_
    @sumitkamble_ Рік тому +2

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

  • @VikasSharma-eg8mc
    @VikasSharma-eg8mc Рік тому +2

    Understood!! Amazing explanation

  • @BhaweshKumawat
    @BhaweshKumawat 9 місяців тому +3

    17:55 l1 > r2

  • @mayanksankhla1538
    @mayanksankhla1538 3 місяці тому

    Great Explanation!
    Just one observation, while explaining solution you considered median to be r1/r2 value in case of odd total length, so left part doesn’t include median, but in coding part you have considered median to be l1/l2 which mean left part includes median. Got little confused with that.

  • @jaypratap3888
    @jaypratap3888 6 місяців тому

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

  • @ArnabBhadra02
    @ArnabBhadra02 Рік тому +4

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

  • @MAHALAKSHMIVEERARAJ
    @MAHALAKSHMIVEERARAJ Рік тому

    Wow explanations. Big Thanks to Striver.

  • @sagark4080
    @sagark4080 Рік тому +5

    @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  Рік тому

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

  • @joeljacob4685
    @joeljacob4685 Рік тому +1

    Excellent explanation!!💌

  • @umabharathis128
    @umabharathis128 2 місяці тому

    Its very helpful and clearly understand...

  • @RGUKTEDUIN
    @RGUKTEDUIN 10 місяців тому

    Understood salute to striver🤓

  • @IamGroot455
    @IamGroot455 4 місяці тому

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

  • @ketanjain5390
    @ketanjain5390 2 місяці тому

    beautifully explained!

  • @maneeshkumarpatel9874
    @maneeshkumarpatel9874 5 місяців тому

    Loved this approach❤

  • @kaichang8186
    @kaichang8186 Місяць тому

    understood, perfect solution

  • @SHUBHAMSINGH-nv7ot
    @SHUBHAMSINGH-nv7ot 8 місяців тому +4

    17:52 l1 is greater than r2 (correction)

  • @GoodLuck-dv2zu
    @GoodLuck-dv2zu 8 місяців тому

    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

  • @welcometoc.s.easpirants
    @welcometoc.s.easpirants 8 місяців тому

    Great explanation. Thank you ❤

  • @RohiniShankari
    @RohiniShankari 4 місяці тому

    love your lectures

  • @charuprabha9696
    @charuprabha9696 5 місяців тому +1

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

  • @animeshkumar2683
    @animeshkumar2683 2 місяці тому

    Happy teachers day !!

  • @nandini62
    @nandini62 4 місяці тому

    Thank you so much broo for these series ☺️

  • @nitishjaswal7564
    @nitishjaswal7564 Рік тому +1

    Thank you Striver sir 🥰

  • @ashwingoel7173
    @ashwingoel7173 9 місяців тому +10

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

  • @techatnyc7320
    @techatnyc7320 2 місяці тому

    Thanks Striver!!

  • @sourabhsoni5065
    @sourabhsoni5065 День тому

    One question to Striver is How did you thought of this Solution. I just though of Merging them and Finding the Median. But want to know How you developed Problem Solving in this way ?

  • @sauravsarkar4785
    @sauravsarkar4785 9 місяців тому

    great explanation buddy. Keep up the good work.

  • @TheKnightTeutonic
    @TheKnightTeutonic 4 місяці тому

    U genius my frnd

  • @ShubhamKumar-uf3gc
    @ShubhamKumar-uf3gc 4 місяці тому

    CLEAN AS ALWAYS

  • @YogaJournalWithMimansa
    @YogaJournalWithMimansa 5 місяців тому

    Amazing Explanation! Thanks!

  • @kratikasharma9824
    @kratikasharma9824 9 місяців тому

    Super cute, at 24:06 not true not true like a very cute kid, Awesome Explanation :) Thank You

  • @abhinav7sinha
    @abhinav7sinha 10 місяців тому

    This is a very nice video and possibly one of the best explanations.
    I just have one suggestion - because you're teaching this online, if you could speak softly, I am sure people would appreciate it. Right now, it seems like you're shouting. I wish you the best, don't take this comment harshly

  • @lalitbisht8381
    @lalitbisht8381 7 місяців тому

    Mind blowing video❤

  • @rajnandinipatel81
    @rajnandinipatel81 10 місяців тому

    brilliant explanation

  • @Bigg_boss_trolls
    @Bigg_boss_trolls 7 днів тому

    extordinary teaching bro

  • @jugnugupta6839
    @jugnugupta6839 Рік тому

    Great explaination..Thank you.💯

  • @nayankhuman1043
    @nayankhuman1043 4 місяці тому

    Understood 😊. You d best ❤

  • @mahakasthurim4998
    @mahakasthurim4998 4 місяці тому +1

    Hi bro . Recently i started to watch your array playlist. Today i watching this median problem . I clearly understood your logic at first time itself . I really amazed with your logical thinking . How could you come up with this logic . You are making me logical thinking and programming more interesting. I am started to learn all the concept . Now i am following logic which is already there. But the way you are approaching all the problem with different optimal solution, its very unpredictable. Please provide some tips how you have build your logic on each ptogram when you started your coding journey. That would help most of the freshers. Thank you!

  • @krishnashukla8099
    @krishnashukla8099 Рік тому

    over the top bhaiya

  • @souvikcseiitk
    @souvikcseiitk 4 місяці тому

    this tutorial is awesome, thanks for this :)

  • @beinginnit
    @beinginnit Рік тому

    Thank you Striver💖💖

  • @asmitraj4740
    @asmitraj4740 Рік тому

    Finally Understood man.

  • @Farhankhan-im3dv
    @Farhankhan-im3dv 8 місяців тому

    the man is not just king he is a ginius