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

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 737

  • @barmalini
    @barmalini 5 лет назад +52

    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

  • @nik220287
    @nik220287 5 лет назад +35

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

  • @shrimatkapoor2200
    @shrimatkapoor2200 4 года назад +252

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

    • @PolkadOfficial
      @PolkadOfficial 3 года назад +11

      @Kayden Khalil Lol, that is scam! fake accounts

    • @nishantkumar1149
      @nishantkumar1149 3 года назад +14

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

    • @JCatharsis
      @JCatharsis 3 года назад +6

      It finally hit me I'm so high rn

    • @nikhileshkumar9126
      @nikhileshkumar9126 3 года назад

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

    • @ncba
      @ncba 2 года назад

      Quite a common phrase

  • @meganlee5897
    @meganlee5897 4 года назад +5

    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

  • @rushivt
    @rushivt 7 месяцев назад +2

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

  • @程龙-b1w
    @程龙-b1w 6 лет назад +8

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

  • @umairalvi7382
    @umairalvi7382 4 года назад +1

    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

  • @shaziasamreen8584
    @shaziasamreen8584 3 года назад +3

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

    • @xendu-d9v
      @xendu-d9v Месяц назад

      7 years now, its fresh

  • @abhijeetpatnaik5494
    @abhijeetpatnaik5494 2 года назад +3

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

  • @dakshays6375
    @dakshays6375 5 лет назад +104

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

  • @balajim7801
    @balajim7801 4 года назад +49

    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 3 года назад +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 2 года назад +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.

  • @DonchenkoNadia
    @DonchenkoNadia 3 года назад +4

    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

    • @gunahawk6893
      @gunahawk6893 2 года назад

      clean implementation good job

  • @sanjitselvan5348
    @sanjitselvan5348 Год назад +1

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

  • @jessica_1811
    @jessica_1811 Год назад +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.

  • @beer_bandhu
    @beer_bandhu 6 лет назад +48

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

  • @wengao6544
    @wengao6544 Год назад +1

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

  • @double_courage57
    @double_courage57 4 года назад +13

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

  • @rkalyankumar
    @rkalyankumar 3 года назад

    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!

  • @uzdik.student
    @uzdik.student 4 года назад +44

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

  • @kimmeng6939
    @kimmeng6939 5 лет назад +79

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

    • @prakashtiwari7834
      @prakashtiwari7834 5 лет назад +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 5 лет назад

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

    • @emilyhuang2759
      @emilyhuang2759 4 года назад

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

    • @sharmamukul938
      @sharmamukul938 4 года назад

      @@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 4 года назад

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

  • @marshallxu7480
    @marshallxu7480 4 года назад +10

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

  • @soulmusic6530
    @soulmusic6530 4 года назад +3

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

  • @remixisthis
    @remixisthis 5 лет назад +4

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

  • @sshangrou8799
    @sshangrou8799 7 лет назад +17

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

  • @somiljain896
    @somiljain896 7 месяцев назад

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

  • @anondoggo
    @anondoggo 2 года назад +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!

  • @maxfeldman6654
    @maxfeldman6654 6 лет назад +11

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

  • @Destiny22377
    @Destiny22377 5 лет назад

    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

  • @stanleyakachukwu7881
    @stanleyakachukwu7881 2 года назад

    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.

  • @SourabhCA
    @SourabhCA 5 лет назад

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

  • @PhoenixRisingFromAshes471
    @PhoenixRisingFromAshes471 4 года назад

    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

  • @bewareofnikhil
    @bewareofnikhil 2 года назад +1

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

  • @ADITYAKUMAR-yd3ow
    @ADITYAKUMAR-yd3ow 7 лет назад +115

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

  • @GHT0310
    @GHT0310 Месяц назад

    im first-year student from Vietnam, thank you a lot for this helpful video, appreciate

  • @vikasaggarwal2834
    @vikasaggarwal2834 2 года назад

    i usually don't comments on videos but your method of explaining made me comment on this...
    Superb video boss..

  • @yoosufahmadh3945
    @yoosufahmadh3945 4 года назад

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

  • @gauravghosh6562
    @gauravghosh6562 Год назад

    this is the best channel for coding hand down

  • @ahasunos5914
    @ahasunos5914 3 года назад

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

  • @arthamsreenivas8858
    @arthamsreenivas8858 5 лет назад +1

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

  • @sanjayizardar2263
    @sanjayizardar2263 5 лет назад

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

  • @jaatharsh
    @jaatharsh 4 года назад

    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.

  • @MykolaDolgalov
    @MykolaDolgalov 5 лет назад +2

    *EDIT:* Thank you for the video. I watched it carefully, then coded the solution from memory. And then I compared to your solution, and mine is a little simpler and easier to understand (code is in the first comment). *Initial comment* 14:41 here it is obvious from the data that we must compare the rightmost and leftmost elements to see if the values in the arrays even overlap in range. If they don't we should skip the binary search and just treat them as one continuous sorted array, and find the median in it directly.
    Thank you for the wonderful videos.

    • @MykolaDolgalov
      @MykolaDolgalov 5 лет назад +1

      A somewhat simpler and easier to follow code:
      public double findMedianSortedArrays(int[] nums1, int[] nums2) {
      int len1 = nums1.length;
      int len2 = nums2.length;
      int res = 0;
      if(len2 < len1)
      return findMedianSortedArrays(nums2, nums1);
      int partitionX = len1 / 2;
      int low = 0;
      int high = len1;
      while(low 0 ? nums1[partitionX - 1] : Integer.MIN_VALUE;
      int num1R = partitionX < len1 ? nums1[partitionX] : Integer.MAX_VALUE;
      int num2L = partitionY > 0 ? nums2[partitionY - 1] : Integer.MIN_VALUE;
      int num2R = partitionY < len2 ? nums2[partitionY] : Integer.MAX_VALUE;
      if(num1L = num2L) { // found correct comb
      if((len1 + len2) % 2 != 0)
      return Math.max(num1L, num2L);
      else
      return 1.0*(Math.max(num1L, num2L) + Math.min(num1R, num2R)) / 2.0;
      } else if(num1R < num2L) { // need to move to the right
      low = ++partitionX;
      } else
      high = --partitionX;
      }
      return Double.MIN_VALUE;
      }

  • @suvirsaurav5587
    @suvirsaurav5587 5 лет назад +19

    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.

  • @saurabhzalke4232
    @saurabhzalke4232 4 года назад +1

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

  • @sharatchandra9198
    @sharatchandra9198 4 года назад

    Most Beautiful explanation and solution. My deepest gratitude to all your videos.

  • @subhodeepganguly2575
    @subhodeepganguly2575 4 года назад

    Very Nice Explanation. One of the best videos of Tushar

  • @seekingWisdomAcrossWorlds
    @seekingWisdomAcrossWorlds 3 года назад

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

  • @stratonov
    @stratonov 2 года назад +2

    Beautiful lovely brilliantly handled edge cases

  • @李克弱
    @李克弱 5 лет назад +1

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

  • @pawandeepchor89
    @pawandeepchor89 6 лет назад +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 !!

  • @韓覲
    @韓覲 3 года назад

    I can never figure it out without this video.

  • @achyutaarjunneupane1154
    @achyutaarjunneupane1154 7 лет назад

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

  • @bharathik6479
    @bharathik6479 4 года назад

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

  • @lukaslehmann9708
    @lukaslehmann9708 2 года назад

    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.

  • @Chandan-io3jm
    @Chandan-io3jm 7 лет назад

    Best Channel for Data Structure && Algorithms !

  • @himangshubiswas542
    @himangshubiswas542 Год назад

    Because of you I am going to solve this problem otherwise I thought I will skip it, thank you

  • @gauravdudeja
    @gauravdudeja 6 лет назад

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

  • @rahul-patil
    @rahul-patil 5 лет назад +17

    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

  • @vishwajeetpawar5074
    @vishwajeetpawar5074 3 года назад

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

  • @mukut5ul
    @mukut5ul 3 года назад

    These videos are great equivalent to full computer science degree content

  • @zaheenrahman6608
    @zaheenrahman6608 7 лет назад +5

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

  • @dzungdev
    @dzungdev 5 лет назад +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 5 лет назад +1

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

  • @_PRITIJHA
    @_PRITIJHA 4 года назад

    Your channel solves all my doubt

  • @shashikantdivekar7839
    @shashikantdivekar7839 4 года назад

    Very good explanation. Thank you Tushar Roy.

  • @Иван315
    @Иван315 3 года назад

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

  • @semurgx
    @semurgx 3 года назад

    this one of the best explanation on youtube, awesome!

  • @pi3ni0
    @pi3ni0 3 года назад +1

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

  • @Avinashkumar-xt4zq
    @Avinashkumar-xt4zq 3 года назад

    Best explanation present on the Internet!

  • @hawaijarjs7496
    @hawaijarjs7496 4 года назад +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!

  • @nabidulalam6956
    @nabidulalam6956 4 года назад

    Simple and elegant explanation. Hats off.

  • @ankush363
    @ankush363 4 года назад

    I got asked this question in an interview .Before this video i ignored this question multiple times due to which i was not able to answer this in interview .After getting rejected from the interview i came here and i feel how can a person come up with such a solution in 45 minute interview and code it properly.But The solution is very nice.

  • @shwetankgupta
    @shwetankgupta 5 лет назад +5

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

  • @JaySolanki91
    @JaySolanki91 6 лет назад +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.

  • @priyanka.sarkar
    @priyanka.sarkar 6 лет назад

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

  • @j50313j50313
    @j50313j50313 4 года назад

    You save my life.
    Just watch for 8 minutes and come up with a solution by myself!

  • @siddharthkhandelwal933
    @siddharthkhandelwal933 Год назад

    Very well explained, the code is very clear and precise. Thanks

  • @DimpleSharma-jo9gf
    @DimpleSharma-jo9gf 4 года назад +1

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

  • @priyankachauhan0112
    @priyankachauhan0112 5 лет назад

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

  • @niksmachnih264
    @niksmachnih264 2 года назад

    The best explanation that I ever heard

  • @35sherminator
    @35sherminator 5 лет назад

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

  • @techbarikcom
    @techbarikcom 4 года назад

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

  • @shubhamgupta9778
    @shubhamgupta9778 3 года назад

    Thank u bro.... Your video always give us very good concept.....
    Please make a session on josephus problem. This problem is very easy to understand but hard to implement.

  • @mrinmoypaul6810
    @mrinmoypaul6810 3 года назад

    Tushar Sir tumi Bhogowan, Love from West Bengal

  • @manojamrutharaj9071
    @manojamrutharaj9071 3 года назад

    Thanks Tushar for the detailed explanation.

  • @PankajSingh-y3u
    @PankajSingh-y3u Год назад

    very good explanation of the concept here, keep make kind of this videos... thank you

  • @prasadwants
    @prasadwants 7 лет назад +127

    bro tushar please make a video on how to prepare for giant companies like apple,amazon etc. also thanks for the amazing video

    • @tusharroy2525
      @tusharroy2525  7 лет назад +76

      I will try.

    • @83rossb
      @83rossb 7 лет назад +54

      What do you think this entire channel is for?

    • @Roger-rf7yp
      @Roger-rf7yp 7 лет назад +6

      Thank you for sharing! This hard problem on Leetcode is understandable by any folk with the basic knowledge of median and binary search! That's really crystal clear explanations!

    • @RAHULRAJ-rv4ng
      @RAHULRAJ-rv4ng 6 лет назад

      Tushar Roy - Coding Made Simple
      Sir, please make some more videos on binary search, and explain the strategy how to apply binary search, and please provide the explanation of the code on the github, the circular binary search problem on the github.
      Thanks

    • @clumsycoco
      @clumsycoco 6 лет назад +2

      @@83rossb Lmao!

  • @SonuSonu-tk5pk
    @SonuSonu-tk5pk 7 лет назад +17

    very neat and excellent explanation

  • @angadthandi4136
    @angadthandi4136 4 года назад +2

    Might totally be a noob question:
    You are using:
    "lo = 0, hi = x" with "while lo

  • @tvbuff
    @tvbuff 3 года назад

    You truly are making coding simple !

  • @msteja
    @msteja 3 года назад

    Geniusly explained the algo 🙏🏻👏🏻

  • @qingyufan7416
    @qingyufan7416 4 года назад

    Best explanation with examples. Thx a lot.

  • @കള്ള_ചക്കോ
    @കള്ള_ചക്കോ 4 года назад +1

    give this man a medal

  • @NeoMennon
    @NeoMennon 5 лет назад

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

  • @VsEdits59
    @VsEdits59 2 года назад

    that was so cool explanation sir !! handsdown🙌🙌

  • @jacobstech1777
    @jacobstech1777 3 года назад

    Simply the best explanation.

  • @jatintayal4010
    @jatintayal4010 6 месяцев назад

    Such an elegant explanation !!

  • @jinny5025
    @jinny5025 4 года назад

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

  • @ramizrizwan3057
    @ramizrizwan3057 Год назад

    such an incredibly good video. amazing explanation

  • @ujjvalsharma5055
    @ujjvalsharma5055 3 года назад

    One of the best explaination!!. Great work. Keep making more video :)