Median of 2 Sorted Arrays of different sizes | Binary Search | Data Structures and Algorithms

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐝𝐞𝐭𝐚𝐢𝐥𝐬 𝐚𝐧𝐝 𝐞𝐧𝐫𝐨𝐥𝐥 𝐟𝐨𝐫 𝐨𝐮𝐫 𝐋𝐈𝐕𝐄 𝐜𝐨𝐮𝐫𝐬𝐞𝐬 𝐡𝐞𝐫𝐞-
    www.educosys.com
    The video contains following parts-
    0:00-1:00 - Question Introduction and example
    1:00-3:00 - Brute Force approach with small optimization
    3:00-8:15 - Logic behind need of partition
    8:15-9:35 - Condition for correct partition
    9:35-12:25 - Changing wrong partitions to correct one
    12:25-15:53 - How to find partition - Binary Search
    15:53-22:14 - Code
    22:14-28:30 - Dry run for example
    28:30-30:00 - Complexity
    30:00-31:08 - Odd number of elements case
    #dsa #softwaredeveloper #interviewquestions

Komentáře • 429

  • @KeertiPurswani
    @KeertiPurswani  Před 3 lety +80

    Hi guys,
    Should have covered one more point - you should apply binary search on the smaller array only otherwise you will end up getting error. I have covered the point of exchanging the arrays at around 29:30 but didn't exactly say that this is a must do otherwise will lead to error.
    Thanks to so many of you who tried writing the code and pointed it out.
    I left this point for the end coz I thought it would be confusing in the beginning but I should have put more emphasis on this.
    Thank you and happy coding! ❤️

    • @shubhamagarwal2998
      @shubhamagarwal2998 Před 3 lety

      a=[100000]
      b=[100001] i guess it won't work bcz
      l1=-inf , r1= 100000
      l2=100001 r2=+inf
      so (l2

    • @shubhamagarwal2998
      @shubhamagarwal2998 Před 3 lety +3

      oh it is working my bad...... i was putting "hi=n-1" but as soon as I put "hi=n" it worked ... can't caught this annoying things in binary search...

    • @abhijithkashyap4537
      @abhijithkashyap4537 Před 3 lety +1

      Can you explain the reason for the error or the scenario where you get the error. In this video you do the binary search on the bigger array and it seems to work correctly, isn't it?

    • @gamingblast2325
      @gamingblast2325 Před 3 lety

      Mam can u explain why there is error when you run binary search on larger array ?

    • @Aditya-pl3xg
      @Aditya-pl3xg Před 3 lety +1

      @@abhijithkashyap4537 because there is possibility that cut2 will go out of bounds if we take larger array for binary search.

  • @battlecrusecruse9155
    @battlecrusecruse9155 Před 3 lety +61

    avoiding this question from the past 3-4 months , and finally found this beautiful explanation .

  • @sarthakagarwal8499
    @sarthakagarwal8499 Před 3 lety +74

    Great Explanation!!
    I watched 4-5 videos of the same problem but still found it hard to understand but she made it clear.
    When I was about to exit the video, she said not to be worried as it is a hard one and stay till the end, so I stayed and I am glad.
    Also, timestamps were a great add on, not many creators bother to put them.
    One more thing, pls don't dislike(if you can't like) this video as it is almost half an hour long video, just imagine how much effort she must have put in this.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +15

      Thank you SO much Sarthak!! ❤️❤️ That is so sweet of you!
      I am glad you stayed till end. While saying that stay till end I was wondering if I should say or not, but so glad that I said it 😇

  • @sudesh6807
    @sudesh6807 Před 9 měsíci +6

    Folks who are finding it difficult please watch multiple videos by different CZcamsrs and visit back again. Hard problem require different perspective and examples to understand properly.

  • @harishchandrasekaran7839
    @harishchandrasekaran7839 Před 3 lety +16

    Didn't understand the first 10 mins when I was watching this in the evening with a disrupted mind. Sat down at 6am the next morning with a calm mind and got everything! Great explanation! Keep up the good work!

  • @siddhantsharma8066
    @siddhantsharma8066 Před 2 lety +2

    Amazing Explanation!!
    You always make sure that each and every aspect of the question is covered properly.
    Thanks a lot!! :)

  • @nehadeshpande6119
    @nehadeshpande6119 Před 2 lety +7

    I could not find a better explanation than this on the whole internet, for this problem! Thank you, Keerti :)

  • @shivangsharma4873
    @shivangsharma4873 Před 3 lety +2

    Great explanation, In the starting I didn't understand much but till the end when you dry run the test case the solution is crystal clear by itself. Thank you for so patient and detailed explanation.

  • @satyaadityabaratam35
    @satyaadityabaratam35 Před 2 lety +1

    crisp and clear. Most importantly you are just addressing what a viewer might get struck at. Thank you so much

  • @adarshkumar-ys9tp
    @adarshkumar-ys9tp Před 3 lety +5

    [2,3,4,5]
    [1]
    some modifications in the code...
    *we need to keep n1< n2, if not pass the arrays in the function to make it n1

  • @roshanmhatre8810
    @roshanmhatre8810 Před 3 lety +4

    Very detailed and most clear explanation🔥 for this question on CZcams, Could you please make an explanation for the same question but array with same sizes 🙏

  • @Perldrummr
    @Perldrummr Před 3 lety +1

    Thank you very much for this explanation! I'm sure I'll be going back over it once or twice before many interviews.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Hope you like other videos as well. Thank you ❤️

  • @shubhamchandra9258
    @shubhamchandra9258 Před 2 lety

    Amazing explanation. This really is quite tough question for a lot of people. You explained the solution with great ease and clarity. Looking forward to more such amazing content. Thanks.

  • @sleepypanda7172
    @sleepypanda7172 Před 2 lety +1

    I liked it a lot. You are really good at explaining the logic. It was like I was thinking along with you. Looking forward to watch more videos whenever I feel stuck at LC. Kudos to your efforts.

  • @SunilGupta-qx6xe
    @SunilGupta-qx6xe Před 3 lety

    Thanks for explaining!
    You make this question very simple.

  • @madhusaivemulamada3556

    Thanks for sharing your knowledge. One of simplest explanations I have seen. Keep up the good work 👏

  • @prashantmohan2233
    @prashantmohan2233 Před 3 lety +3

    This was literally the best explanation of this problem available on youtube.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thanks Prashant. Means a lot 😇
      I hope you like rest of my content as well 😊

  • @shristip519
    @shristip519 Před 3 lety +7

    12:39 Thank You so much, this was needed. This problem has an expert tag in gfg. I couldn't follow Tushar's explanation although it was my incompetence but finally I understood this. Thank you for your effort!

  • @chaitrapvasista7041
    @chaitrapvasista7041 Před 2 lety +2

    Very detailed explanation and the diagrams actually helped visualize the solution!

  • @balajimysore7732
    @balajimysore7732 Před 2 lety +1

    HI Keerti, beautifully explained first the intuition and then the approach to write the code, starting with binary search and then optimizing. Please keep doing these amazing videos of hard problems. You rather communicate very clearly😀

  • @iSumitYadav
    @iSumitYadav Před 3 lety +1

    Just wow! Nicely explained 👌🏻
    Subscribed with All notifications turned on!👍🏻

  • @rajraorane7338
    @rajraorane7338 Před 3 lety

    thanks for being so patient and reiterating imp points again.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Hope you like rest of the videos as well! 😇😇

  • @Francis12345ify
    @Francis12345ify Před rokem

    I loved this solution, I loved how you labelled everything on the board. Made it so much easier to process. Thanks!

  • @sajalpushpad
    @sajalpushpad Před 3 lety +1

    You are doing such a great job. It helps alot. Thanks for this amazing explanation :)

  • @debaratighatak2211
    @debaratighatak2211 Před 3 lety +2

    I was stuck with this problem since several days,finally understood the solution.Thanks for such an amazing and detailed explanation! :) ❤

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +3

      So glad toy understood it ❤️❤️
      Hope you like rest of the videos as well. Please do share the channel with your friends. It would mean a lot to me 😇😇

  • @harshitakanal7411
    @harshitakanal7411 Před 3 lety

    Great Explanation, Thank you so much!

  • @bluemeet8546
    @bluemeet8546 Před 3 lety

    thanks a ton for such a wonderful explanation of hard problems

  • @deviprasadchand
    @deviprasadchand Před 2 lety

    This is hands down the best explanation to this problem....thank you.

  • @SonuKumar-sf1ch
    @SonuKumar-sf1ch Před 3 lety

    Amazing Explanation!!
    Thank you so much!
    This video is very helpful.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you. Hope you like rest of the videos as well 😇❤️

  • @rahulbhati3079
    @rahulbhati3079 Před 3 lety +6

    BEST EXPLANATION, REALLY IT IS AMAZING. EVEN FROM YOUR HARDWORK I GOT MOTIVATED .

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you SO much ❤️❤️ means a lot 😇😇

  • @lokeshnegi5051
    @lokeshnegi5051 Před 3 lety

    finally i get to understand this algo.
    really great explanation ,this video deserves more views.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thanks Lokesh. Please do share the channel with your friends and help it get better reach 😇
      Hope you like rest of the videos as well. Keep sharing your feedback!

  • @chloetang5839
    @chloetang5839 Před 2 lety

    brilliant solution and very helpful explanation! thank you for doing this video

  • @rhitamdutta1996
    @rhitamdutta1996 Před 2 lety +1

    The best video on this specific problem, subbed!

  • @sushmitagoswami2033
    @sushmitagoswami2033 Před rokem

    I just like how you explain things with so much calmness. Kudos Keerti!

  • @namansharma2320
    @namansharma2320 Před 3 lety +5

    You have explained very well Ma'am. Please upload more videos of data structures and algorithms. And thanks for explaining this problem in a simple way.

  • @shivanimehta5280
    @shivanimehta5280 Před 3 lety +1

    Procrastinating the problem for so long, but not anymore. Just crystal clear 🙏

  • @parthgupta3799
    @parthgupta3799 Před 2 lety

    Excellent Explanation!! Thank you!

  • @johnnyf1624
    @johnnyf1624 Před 2 lety

    thank you, your explanation is the best. it was rlly good when you applied the test case

  • @anshumansharma4580
    @anshumansharma4580 Před 3 lety

    Terrific Explanation, Ma'am. Crystal Clear!!!!

  • @mr.curious1329
    @mr.curious1329 Před 3 lety

    You can make as good as a teacher as you do as a software engineer!!
    You have the flair for teaching!!
    Very good!

  • @vershaverma1409
    @vershaverma1409 Před 3 lety

    Loved the explanation !! Thankyou so much. :)

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thanks Versha! Hope you like rest of the videos as well 😇

  • @sameekshabhatia2219
    @sameekshabhatia2219 Před 3 lety +1

    Thank you so much for explaining it .It helped me a lot .Really appreciate your efforts.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thank you Sameeksha. Hope you like rest of the videos as well ❤️😇

  • @syedhabeebuddin101
    @syedhabeebuddin101 Před 3 lety

    very well Explained....It's really Amazing !

  • @dikshamittal5827
    @dikshamittal5827 Před 2 lety

    Hi Keerti ! I have recently started seeing your videos, for this one specially I must say you have just done an awesome job.

  • @himanshugupta7010
    @himanshugupta7010 Před 2 lety

    seriously escaping from that question from past 2-3 months 'coz the explanations avail were very confusing . thank you so much for explaining with such a great precision.

  • @souravsuman2633
    @souravsuman2633 Před 2 lety

    Thanks for the such a nice explanation, really helpful.

  • @prathamgupta992
    @prathamgupta992 Před 2 lety

    Thankyou so much. I was stuck at this problem for weeks.

  • @cpwithsundar
    @cpwithsundar Před 3 lety +2

    WoW!
    All i can see is 5-6 hrs (even more than that) of hard work just to make us students understand a complex and hard solution in 30 minutes.
    It was fantastic ! Loved your in-depth explanation and analysis.

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thank you so much, and honestly it's much more than 5-6 hours 😅😅
      Hope you like rest of the videos as well!! 😇😇

    • @ceciljoel9577
      @ceciljoel9577 Před rokem

      @@KeertiPurswani how long did it take you to solve this problems completely? is it reauired to he gennady level in software dev or not?

  • @pankajkhushalani
    @pankajkhushalani Před 2 lety +2

    Thank you so much for the explanation! I was stuck thinking how can I reduce O(n1 + n2) to O(log(min(n1,n2))) for so long!

  • @diveshrajput572
    @diveshrajput572 Před 3 lety +2

    Such a awesome and cleared explanation . Thank you so much ma'm.

  • @Chetan.Kothari
    @Chetan.Kothari Před 3 lety

    Thank you!!! Good Explanation!!!

  • @VIJAYKUMAR-tn1kr
    @VIJAYKUMAR-tn1kr Před 3 lety

    Really the best Effort.Thank you so much for this

  • @kirankutte7073
    @kirankutte7073 Před rokem

    Hello @keerti Purswani, atlast why we have to consider the smaller length. Because i ran the same code with the greater length ,its not working . Could you please me tell why. Thank in advance !

  • @soni.himansh
    @soni.himansh Před rokem

    Great Explanation!!

  • @sakshisatija8929
    @sakshisatija8929 Před 3 lety

    You made it so simple. Thank You!

  • @NikhilKekan
    @NikhilKekan Před 2 lety

    becomes clearer and clearer slowly, thanks

  • @shiwani9146
    @shiwani9146 Před rokem

    thank you !! great explanation

  • @akashprajapati7988
    @akashprajapati7988 Před rokem

    Thanks mam I was trying to understand this ques from many months. Now I've understood properly.
    Great explanation ☺️☺️☺️

  • @rishabhkesarwani5761
    @rishabhkesarwani5761 Před 3 lety

    Very beautifully explained !! No doubts now .. Kudos

  • @shikharsrivastava8160
    @shikharsrivastava8160 Před 3 lety

    Keerti Di, thank you so much, for such a great and easy explanation for such a difficult question. You have really helped a lot. Thanks a lot 🙂

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thank you so much Shikhar. Means a lot to me❤️😇
      Please do share the channel with your friends. It will really help me be more motivated and create more😊😊

  • @sujoyseal195
    @sujoyseal195 Před rokem

    Easy and helpful ! Thanks

  • @royarijit998
    @royarijit998 Před 3 lety +1

    Thank you so much for the video ma'am! I remember requesting this video a while ago, I'm glad you accepted this question :)

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Hope you liked the explanation!
      Trying to cover all the requested questions 😊

    • @brickcommander
      @brickcommander Před 3 lety +1

      @@KeertiPurswani cses.fi/problemset/task/2216 ... please solve this problem it's solution is not available on internet

    • @royarijit998
      @royarijit998 Před 3 lety +2

      @@brickcommander I've added comments to my solution (pastebin.com/UtbFiUkY ) for the question, I hope it helps.

    • @brickcommander
      @brickcommander Před 3 lety +1

      @@royarijit998 thankyou sir, you saved me

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      I just saw these comments! SO SO happy to see people helping each other out. Thank you Arjit. Super proud of you ✌️✌️

  • @nurshuvo3854
    @nurshuvo3854 Před 2 lety

    Absolutely amazing explanation.

  • @gautamjangir8927
    @gautamjangir8927 Před 2 lety +1

    The best explanation in the entire youtube.....Thanks.

  • @ravitejachidurala1146
    @ravitejachidurala1146 Před 3 lety

    Best Explanation of all the recommended videos✌👍

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you so much. Hope you like rest of the videos on the channel as well ❤️😇

  • @sudiptochakraborty4929

    Thank you so much for the video!

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you for the comment and for the support 😇❤️

  • @ibknl1986
    @ibknl1986 Před rokem

    A very good explanation. Thank you.

  • @venkateshelangovan5381

    excellent explanation :) best explanation available in YT :) Thank u :)

  • @ChetanMishra-id8er
    @ChetanMishra-id8er Před 3 lety

    dry run clear all the intuition behind that stuff good job

  • @khushable
    @khushable Před 3 lety

    You have explained with so much patience...kudos to that..i will need to re-watch once again to understand fully...here's to understanding better ... cheers

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you. Hope you understood everything!! 😇😇

  • @user-cw3jg9jq6d
    @user-cw3jg9jq6d Před 3 měsíci

    One of the best explanations I got online. Thank you.

  • @MrJackytonny
    @MrJackytonny Před 2 lety

    Thanks a lot for the great Explanation! May I know why hi has to be set to the length of the array? instead, I used to set hi = len(arr) - 1 in a Binary Search. So could you please explain this point as well?

  • @soorya3562
    @soorya3562 Před 3 lety

    amazing explanation ma'am !!! one wouldn't find a better one

  • @kshitijgaur9635
    @kshitijgaur9635 Před 3 lety

    Amazing explanation, dry run helped a lot

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thank you! Hope you like rest of the videos as well 😇😇

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

    I watched 3 different videos for the same problem.
    This is the best crystal clear explanation. Kudos to you. Here is the Java code I have written.
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    if (nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1);
    int m = nums1.length, n = nums2.length;
    int low = 0, high = m;
    while (low r2) {
    high = mid1 - 1;
    } else if (l2 > r1) {
    low = mid1 + 1;
    } else {
    return (m + n) % 2 == 0 ? (Math.max(l1, l2) + Math.min(r1, r2)) / 2.0 : Math.min(r1, r2);
    }
    }
    return -1;
    }

  • @sainikhil956
    @sainikhil956 Před 2 lety

    i almost did the code but i have taken hi=n-1 and did something like that
    but how did you figure it out that we have to use h1=n ?

  • @mahee96
    @mahee96 Před 2 lety +4

    Notes for myself:
    Concept:
    Find approximate positions where the two lists are grouped into left and right
    such that max(left)

  • @saurabhparwani9874
    @saurabhparwani9874 Před 3 lety +1

    Finally understood the whole solution & intuition.
    Thanks a lot for clear & detailed explanation.👌

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      So glad it helped! Hope you like rest of the videos as well 😇😇

    • @saurabhparwani9874
      @saurabhparwani9874 Před 3 lety

      @@KeertiPurswani Of Course Mam .
      Even I have sent you connection request on LinkedIn.

  • @chkmystyle
    @chkmystyle Před 3 lety

    Awersome explanation...seen multiple videos but this was the best

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Yaaaay. Thank you❤️❤️
      Please do share the channel with your friends 😇😇

  • @prasantharavindramesh1078

    Hi keerthi.Fantastic teaching.ill try out the code today with even and odd sized arrays.Can i use the same approach to find out median of 3 sorted arrays?

  • @ranbirsingh4472
    @ranbirsingh4472 Před 2 lety

    Very well explained Keerti Di.Thanks for making video on this .I have gone through lot of articles and videos. Finally got clarity after watching this wonderfull video. Thanks a lot.The way you are helping us is really appreciable. Wish you to stay healthy and keep posting such videos.

    • @KeertiPurswani
      @KeertiPurswani  Před 2 lety +1

      Thank you so much Ranbir. It really means a lot 😇😇🙏🏻🙏🏻

  • @shikharathaur5179
    @shikharathaur5179 Před 2 lety

    Amazing explanation !

  • @DebabrataDas-xl5or
    @DebabrataDas-xl5or Před 11 měsíci

    Thank you so much for such a beautiful explanation
    Finally I realized why we are doing what we are doing.

  • @saicharanreddy6379
    @saicharanreddy6379 Před 2 lety

    the best explanation of all the videos on the same problem.

  • @andriidanylov9453
    @andriidanylov9453 Před 2 lety

    Great. Thanks for giving nice lesson

  • @sankalpchapalgaonkar3137

    Amazing video Keerti. Really appreciate. Very well explained.

  • @gloriaben01
    @gloriaben01 Před 3 lety

    I have seen so many confusing videos of the same problem, but you explained it really well. Thanks

  • @AlokRatnaparkhi
    @AlokRatnaparkhi Před 3 lety +1

    Best explanation on the internet..more power to you👌

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      Thank you so much. Please do consider sharing the channel with your friends 😇

  • @gauravsrivastava3040
    @gauravsrivastava3040 Před 3 lety +1

    You got awesome teaching capabilities. This explanation was truly in-depth and very helpful. Thanks a ton, Keerti. Please do keep teaching on this channel, even after you clear fb/google etc :)
    P.S another good candidate for Binary Search: Split array largest sum

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety +1

      That one is on my list already 😊😊
      Thanks for the support! 😇

  • @user-st2gz7zl9q
    @user-st2gz7zl9q Před 9 měsíci

    this is very great explaination... thank yo so much

  • @rajatgoyal8155
    @rajatgoyal8155 Před 3 lety

    very nice explanation
    Thank you so much for this video

  • @shivanikuchhal2314
    @shivanikuchhal2314 Před 3 lety

    Such a great explanation! Thanks a lot Keerti!

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      Thank you Shivani. Hope you like rest of the videos as well 😇❤️

    • @shivanikuchhal2314
      @shivanikuchhal2314 Před 3 lety

      @@KeertiPurswani you're amazing Keerti! Believe me, the questions for which I have watched your explanation, all those questions explanation is pasted in my mind. I can never forget the explanation. One day, I was solving find the running median question in some interview, and I your entire video was playing in my mind during the interview! Keep up the good work! You inspire me! :)

    • @KeertiPurswani
      @KeertiPurswani  Před 3 lety

      That's so nice of you Shivani. Your comment made my day. Thank you ❤️

  • @mystryb345
    @mystryb345 Před 3 lety

    Very good explanation I was no able to understand after watching too many video.Now everything is crystal clear

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

    This is the best explanation for this problem. understood it.

  • @uditasen4411
    @uditasen4411 Před 3 lety

    I never understood this, and median of the stream questions before your explanation video keerti ma'am. Thank You and Please make more videos on such hard problems.

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq Před 3 lety

    Can you explain the reason for the error or the scenario where you get the error. In this video you do the binary search on the bigger array and it seems to work correctly, isn't it?

  • @praveenj3112
    @praveenj3112 Před 3 lety

    Could you please share me the code link ? I am getting few execptions while running the code

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

    thank you so much!! extremely well explained

  • @kajawali7516
    @kajawali7516 Před rokem

    It is the easiest explanation in whole CZcams, u teach important thing "we need why to understand how".

  • @ishaankulkarni49
    @ishaankulkarni49 Před 2 lety

    really well explained. thanks :)