Binary Search : Median of two sorted arrays of different sizes.

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • In example 1 end should be 5(not 4) since there are total 5 elements.
    Find median of two sorted arrays of different sizes. Algorithm is based on binary search.
    github.com/mission-peace/inte...
    / tusharroy25

Komentáře • 726

  • @shrimatkapoor2200
    @shrimatkapoor2200 Před 3 lety +237

    I love when he says "once it hits you" like some kind of drug or something

    • @alienwareCL
      @alienwareCL Před 3 lety +11

      @Kayden Khalil Lol, that is scam! fake accounts

    • @nishantkumar1149
      @nishantkumar1149 Před 3 lety +13

      It's a better than a drug and the dopamine released on solving questions is sickkkk.

    • @JCatharsis
      @JCatharsis Před 2 lety +6

      It finally hit me I'm so high rn

    • @nikhileshkumar9126
      @nikhileshkumar9126 Před 2 lety

      don't do drugs bro. It's harmful.

    • @ncba
      @ncba Před 2 lety

      Quite a common phrase

  • @dakshays6375
    @dakshays6375 Před 5 lety +85

    Damn !!! How did anyone come up with such an Algorithm

  • @nik220287
    @nik220287 Před 4 lety +31

    Man the way you explained it in the first 5 minutes just clarified everything. Thanks. Great video!

  • @user-bv6bi2sz8r
    @user-bv6bi2sz8r Před 5 lety +7

    I was plagued by this problem for a very long time, you made it crystal clear, sir. Thank you!

  • @sshangrou8799
    @sshangrou8799 Před 6 lety +16

    thanks very much.it took me long time to think the algorithm on leetcode, your explanation is so clear and concise.Very nice

  • @video-vocabulary
    @video-vocabulary Před 3 lety +42

    00:00 Introduction
    01:39 Solution
    06:19 Example 1
    14:35 Example 2
    20:11 Code

  • @beer_bandhu
    @beer_bandhu Před 6 lety +45

    This is amazing. I was so confused by this problem, you explained it so succinctly. Thanks

  • @suvirsaurav5587
    @suvirsaurav5587 Před 5 lety +18

    In the first example, the end value at the start should be 5 as we can have a partition when all the 5 elements of array X will be on the left side.

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

    I can't stress enough, how clear this video explanation is. Really loving your work, and it's very very helpful.

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

    after reading multiple explanations, this one finally made me understand how to think.
    Thanks for the video.

  • @kimmeng6939
    @kimmeng6939 Před 5 lety +76

    7:18 end should be 5 not 4 because we are searching which position we want to cut.

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

      YES! Had the last element of first array been 10 instead of 15, the algorithm would've broken at 13:45. Anyways, the explanation was awesome.

    • @everydayleetcode2961
      @everydayleetcode2961 Před 4 lety

      @@prakashtiwari7834 I was wondering why did he take end as 4 and read this comment ! Thanks a lot !

    • @emilyhuang2759
      @emilyhuang2759 Před 4 lety

      But why not 4? The 5th index is empty....so I would think the end is 4...?

    • @sharmamukul938
      @sharmamukul938 Před 3 lety

      @@emilyhuang2759 Exactly even i'm not able to figure out why the end should be 5 and not 4 as it is clearly mentioned in the video that first array has 5 elements in total.

    • @varunwalia95
      @varunwalia95 Před 3 lety

      @@sharmamukul938 it should be the size of the array not index that is to be taken as high element according to code.

  • @maxfeldman6654
    @maxfeldman6654 Před 6 lety +10

    probably one of the best explanations i have seen so far, SUBSCRIBED!

  • @balajim7801
    @balajim7801 Před 4 lety +47

    Clear explanation. Thanks.
    A small error, 8:33 value of "end" should be 5 (size of small array). I followed just your example and coded, everything was right but failing always, until I watched your code & realized that end should have initial value of smallest array's length.

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

      any clear explanation on why would we do it like this? is it the same reasoning behind adding + 1 to the sum of both arrays before dividing by two?

    • @haoyuwu894
      @haoyuwu894 Před 2 lety +5

      @@eskuAdradit0 I think it's because in some cases we can have all the elements in x on the left side. He mentioned that partitionX is "the number of elements partitioned to the left in x." So in those cases, partitionX can be the size of the small array. Consider this example: arrayX: [1, 2, 3, 4] and arrayY: [5, 6, 7, 8]. All the elements in x are smaller than elements in y. Hope this helps.

  • @arthamsreenivas8858
    @arthamsreenivas8858 Před 5 lety +1

    Tushar Sir, you literally saved my time with crystal clear explanation on this problem.

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

    Brilliant video and explanation!
    One minor change to the 1st example:initialization:
    1) pick smaller len
    2) start = 0, end = len, binary search the len of left size of X
    e.g with the first example:
    1st round lo = 0, hi = 5, mid = lo + (hi - lo) / 2 = 2
    2nd round lo = mid + 1 = 3, hi = 5

  • @yoosufahmadh3945
    @yoosufahmadh3945 Před 4 lety

    Repeat watching the first 6 minutes until you get it. Totally helps. Thank you so much. You simply put it

  • @sanjayizardar2263
    @sanjayizardar2263 Před 4 lety

    Perfectly explained... Found most useful among all the available videos on CZcams for median in two sorted arrays.

  • @marshallxu7480
    @marshallxu7480 Před 4 lety +9

    this is the best video of all time, it brings happiness. thank you!

  • @user-vb2rm9hl9d
    @user-vb2rm9hl9d Před 5 lety +1

    this is the best explan I ve ever seen
    step by step with details,
    thank you

  • @rkalyankumar
    @rkalyankumar Před 2 lety

    The best possible explanation of the problem I've seen so far. I have now clear idea on how median of two sorted array algorithm works using binary search & partitioning arrays. Good stuff!

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

    Beautiful solution, thank you so much! I can't believe how close I was to the solution, I wished I pushed myself a bit harder, but this was a great educational experience. Thanks again!

  • @barmalini
    @barmalini Před 5 lety +46

    I paused the video at 20:12 and now going to solve the problem at leetcode myself, thank you so much Tushar, your help for the rest of us who have never graduated from the CS but still want to become a sane programmer is invaluable

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

    What a precise and correct explanation of the algorithm! Thanks for making and sharing this video, Tushar.

  • @remixisthis
    @remixisthis Před 5 lety +4

    Very well done detailed explanation. Much better than the LeetCode solution example. Thanks for taking the time to make this!

  • @DimpleSharma-jo9gf
    @DimpleSharma-jo9gf Před 3 lety +1

    Such a great explanation! Love how you simplify things. Thanks a lot!!!

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

    Even after 5 years your video is best for this problem.Thank you so much for wonderful Explaination

  • @rushivt
    @rushivt Před 24 dny

    I have seen many other videos for this problem, but yours is the one that made me follow every step clearly. Thank you!

  • @JaySolanki91
    @JaySolanki91 Před 5 lety +1

    Simply Amazing !!! This algorithm was really helpful and you gave good examples to solve it. Especially the one in which you covered the corner cases.

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

    So elegant and simple. One of the best tutorial ever! Keep it up.

  • @dzungdev
    @dzungdev Před 4 lety +1

    Thank you very much for your great explanation. I learn a lot from you, not only the code but the way to explain the problem.

  • @dongshuowu3454
    @dongshuowu3454 Před 5 lety +1

    Thank you for the wonderful video. I was stuck for a long time until you made it co clear to me.

  • @35sherminator
    @35sherminator Před 5 lety

    Legendary. Very crisp and clear approach. Thank you very very much!

  • @ADITYAKUMAR-yd3ow
    @ADITYAKUMAR-yd3ow Před 6 lety +111

    Perfect explanation, initially I though it would be difficult for me. But gradually you made me understand in only one go.
    Thanks 😀

  • @sanjitselvan5348
    @sanjitselvan5348 Před rokem

    I went through 5 videos before landing here and finally understanding the intuition behind the solution. Thank you!

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

    Thanks Bro for the excellent explanation! It is crystal clear now how the logic works for both odd/even scenarios. Appreciate your time. Cheers!

  • @zaheenrahman6608
    @zaheenrahman6608 Před 6 lety +5

    My boy Tushar Roy! Earned yourself a sub! Gonna help my algo course so much

  • @double_courage57
    @double_courage57 Před 3 lety +11

    @4:30 - I took a second to understand this : Think of the final sorted array (without duplicates) and draw a median line. We need to find the average of numbers which are immediately to the left and right of that median line. If x2 is immediately to the left of the median line, it has to be greater than y5 (remember final array is sorted). Similarly, if y6 is to the right of the median line it has to be lesser than x3. Hope this helps someone!

  • @wengao6544
    @wengao6544 Před rokem

    This video is definitely the most effective one in providing a clear explanation of the algorithm

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

    The best explaination with perfect testcases that I found for this question on Internet.
    Thank you!

  • @pawandeepchor89
    @pawandeepchor89 Před 6 lety +2

    Excellent !! Nothing more one can do to explain this problem :)
    Thanks for sharing it buddy !!
    You are a star, keep up the good work, cheers !!

  • @SourabhCA
    @SourabhCA Před 4 lety

    Spent a lot of time trying to understand this from leetcode solutions and discussion. And you video explained it in 5 minutes

  • @jaatharsh
    @jaatharsh Před 3 lety

    don't think there are words to express my gratitude towards your hard work in making such bloody awesome videos @TusharRoy
    your videos indeed are class apart & blessing for someone who wants to gain in-depth knowledge on DSA.

  • @rogerwhite8061
    @rogerwhite8061 Před 5 lety

    Best explanation of this challenge I have found, thank you so much

  • @lukaslehmann9708
    @lukaslehmann9708 Před rokem

    Hi Sir, thanks for your great and detailed explanation...just had watched serveral videos and explanations before I saw your video...and finally I understood how it worked...It is really awsome that you used these examples to illustrate the problem (including the edge case) and in your code you also wrote so many annotations...I really appreciate your effort and great work.

  • @priyankachauhan0112
    @priyankachauhan0112 Před 4 lety

    Hey Tushar,
    You make complicated problem look so simple and easy. Awesome job.

  • @harishgovindan
    @harishgovindan Před 4 lety

    Excellent video! Quite precise and covers all aspects! Thank you!

  • @gauravdudeja
    @gauravdudeja Před 6 lety

    After watching 2 times it quite easy to understand. Keep up the good work Tushar.

  • @vishwajeetpawar5074
    @vishwajeetpawar5074 Před 2 lety

    Thanks Tushar.Your explanation helped a lot to understand the depth of logic and algorithm behind this question.

  • @dragonflyboss7162
    @dragonflyboss7162 Před 4 lety +1

    This explanation is so detailed, thank you!

  • @vladn.2332
    @vladn.2332 Před 6 lety

    Very clear and good explanation. Thank you very much, Tushar!

  • @MohitSinha4
    @MohitSinha4 Před 4 lety +1

    Wow, your explanations are just great!
    Thank you so much!

  • @umairalvi7382
    @umairalvi7382 Před 4 lety

    People like me wouldnt understand this video in one time,you gotta watch it multiple times ,read about the problem solving strategy then you will get the problem.
    He has explained in a very good manner

  • @shashankshekharkaushik1567

    I was so confuse how to solve this without taking care of corner cases as extra, your explanation was awesome. Thanks

  • @ahasunos5914
    @ahasunos5914 Před 2 lety

    Thank you so much Tushar. This explanation will last for generations.

  • @Destiny22377
    @Destiny22377 Před 4 lety

    Great solution. Not every detail was explained but as everyone else was saying this is probably the best solution on the net and if not it is one of the best ! The missing details can be inferred

  • @simingye7502
    @simingye7502 Před 5 lety

    Best explanation I have ever seen! Thank you!

  • @SonuSonu-tk5pk
    @SonuSonu-tk5pk Před 6 lety +17

    very neat and excellent explanation

  • @VishwajeetPandeyGV
    @VishwajeetPandeyGV Před 5 lety

    Thanks for this. You made it very clear & easy & fast to understand

  • @Chandan-io3jm
    @Chandan-io3jm Před 6 lety

    Best Channel for Data Structure && Algorithms !

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

    Tushar, thank you so much! You explained it perfectly!
    If someone need solution in Python, here it is:
    class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
    if len(nums1) > len(nums2):
    return self.findMedianSortedArrays(nums2, nums1);

    x = len(nums1)
    y = len(nums2)
    low = 0
    high = x
    while low

  • @jessica_1811
    @jessica_1811 Před rokem +1

    came to "like" this video only found out that I have "liked" this video 4 years ago when I was looking for my last job. Thank you Tushar.

  • @akshaysuman8168
    @akshaysuman8168 Před 6 lety +1

    Its so nice to see you back

  • @mccloud35
    @mccloud35 Před 6 lety

    This explanation is fantastic. Great job Tushar!

  • @matthkang
    @matthkang Před 5 lety

    Thank you for making it clear and easy to understand!

  • @krishna0908
    @krishna0908 Před 6 lety

    Excellent explanation, great clarity. Thanks Tushar.

  • @NeoMennon
    @NeoMennon Před 5 lety

    Tushar you are awesome! I have watched few video about this question, yours is the most intuitive one

  • @achyutaarjunneupane1154

    great sir, got it in single explanation, I looked all over for over 4 hr

  • @bharathik6479
    @bharathik6479 Před 4 lety

    Crystal clear explanation , Bro. You are helping a lot of people, Please Keep up the good Work.

  • @vishnup3182
    @vishnup3182 Před 4 lety

    Best explanation found so far. Thanks !

  • @juliahamilton3493
    @juliahamilton3493 Před 4 lety

    Thank you so much for explaining the algorithm! Very clear!

  • @techbarikcom
    @techbarikcom Před 4 lety

    You are making my life a lot easier! May God bless you

  • @priyanka.sarkar
    @priyanka.sarkar Před 5 lety

    Hats off sir...I am really overwhelmed by the way you explained this algorithm and made it so easy and simple...(y) Thank you so much...:-)

  • @shwetankgupta
    @shwetankgupta Před 5 lety +5

    Finally, I understood the crux of this problem. Thanks for the enlightenment.

  • @alejandrofarias7320
    @alejandrofarias7320 Před 5 lety

    Very good videos. You are great instructor. Cheers from México.

  • @nandanimadhukar
    @nandanimadhukar Před 3 lety

    Great explanation with ample examples! Keep up the good work

  • @jinny5025
    @jinny5025 Před 3 lety

    wow there's nothing more than clearer than this! thanks :) you just saved my time

  • @emilyhuang2759
    @emilyhuang2759 Před 4 lety +5

    7:21 What do you mean by +1 allows what to "play well with both odd and even number of elements in the combines array"?

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

      So that we have extra element on the left side , where we are looking for median. listen from 9:59 ,it might help you.

  • @biswajeetchatterjee4721

    Thanks Tushar.. Your videos really help to understand complex algos.. Keep it up and keep posting videos like this :)

  • @nabidulalam6956
    @nabidulalam6956 Před 3 lety

    Simple and elegant explanation. Hats off.

  • @igenyar
    @igenyar Před 3 lety

    Your explanation is so sharp clear. Thanks!

  • @stanleyakachukwu7881
    @stanleyakachukwu7881 Před 2 lety

    Brother, you have done well in explaining this. Thanks! I wanted to abandon this until I saw your video. With what I have known now I can go and write it myself until I pass it without looking at random solutions across the internet. I have subscribed for more.

  • @user-pw1th5et9b
    @user-pw1th5et9b Před 2 lety

    Thank you so much. Your explanation is the best. Only with you I understood how to solve that problem.

  • @junjiechen7341
    @junjiechen7341 Před 6 lety +1

    Very down to earth explaination.

  • @vinayybhore
    @vinayybhore Před 6 lety

    Excellent Solution and the best so far explanation for this problem on youtube :)

  • @britisundarmondal7297
    @britisundarmondal7297 Před 4 lety

    Thank you for the wonderful video. Easy explanation for such a complex problem.

  • @jayachandrann6825
    @jayachandrann6825 Před 6 lety

    Perfect explanation, thanks for your hard work !!!...

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

    Thanks, good explanation. I was looking for different resources, but it was the best so far :)

  • @sindhusp8935
    @sindhusp8935 Před 3 lety

    This is such a neat algo! thanks for the explanation

  • @360nickx
    @360nickx Před 4 lety

    Excellent Break down. Much appreciated.

  • @jimz2392
    @jimz2392 Před 5 lety

    Thank you Tushar, your video helps me a lot!

  • @amirabdou4940
    @amirabdou4940 Před 3 lety

    Excellent walkthrough of the algo and the code. kudos bro

  • @kumarann3483
    @kumarann3483 Před 5 lety

    clear explanation and concise code, good work!

  • @user-hr1tx7bo1w
    @user-hr1tx7bo1w Před 5 lety +3

    Thank you, it's very helpful.

  • @WS-lv4kk
    @WS-lv4kk Před 5 lety +3

    For odd total array size, instead of adding 1 in the (x+y+1)/2 and taking the max of the left half, I think you can just do (x+y)/2 and take min of the right half.

  • @rahul-patil
    @rahul-patil Před 4 lety +16

    15:24 If there are n element then we can partition that array in n+1 no. of ways.
    E.G 4 elements then we partition it in.
    1) 0 - 4
    2) 1 - 3
    3) 2 - 2
    4) 3 - 1
    5) 4 - 0

  • @vishnu_bhatt
    @vishnu_bhatt Před 6 lety

    Helped a lot bro.Thank you for such good explanations.

  • @kulkarnianiket929
    @kulkarnianiket929 Před 5 lety +2

    Great video! I think you need to divide by 2.0 on line 46. It failed when I ran it with input1 as [1,2] and input2 [3,4].

  • @PhoenixRisingFromAshes471

    Jahapana tussi great .........sir i read a lot about this problem solution everywhere but finally came to understand from your video only...you made a very tricky concept so simple to understand

  • @amitkumarjha4010
    @amitkumarjha4010 Před 3 lety

    This was so wonderfully explained. Thankyou so much !!

  • @DhananjayTyagi24
    @DhananjayTyagi24 Před 4 lety

    The best explanation so far.