Median of Two Sorted Arrays - Binary Search - Leetcode 4

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    Twitter: / neetcode1
    Discord: / discord
    Code on Github: github.com/neetcode-gh/leetco...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    Problem Link: neetcode.io/problems/median-o...
    0:00 - Read the problem
    1:45 - Drawing explanation
    14:05 - Coding explanation
    leetcode 4
    This question was identified as a facebook interview question from here: github.com/xizhengszhang/Leet...
    #median #facebook #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 507

  • @NeetCode
    @NeetCode  Před 3 lety +26

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews

  • @stupidfrog
    @stupidfrog Před 5 měsíci +158

    Easily the hardest LC problem I've ever come across to date. This solution makes sense, but there is no way anyone is coming up with this in a 45 min interview lol

    • @user-ib3ev5pl2t
      @user-ib3ev5pl2t Před 3 měsíci +9

      what does an interview even entail? They want an employee who has experience in a certain field and they test it with tasks. Yes, it's a difficult task, but it just shows whether you've had experience of problem solving in general. If there weren't enough neurons to come up with a solution, it means you don't have enough experience. But that doesn't mean you have to memorise every task, you don't have to. You just have to solve a lot of problems and memorise the patterns themselves.
      So if a person was experienced, had enough neurons to solve this problem, then he is fit for the job. If he could not solve this problem, it means that he has not solved similar ones, it means that he does not have a general knowledge of patterns in this area.

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

      Then grind more 😂

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

      @@user-ib3ev5pl2t dude shut up. you would never solve this in 45mins having never seen it before. foh

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

      @@user-ib3ev5pl2t A yes cause obviously having experience in the technologies used in the real problems is less important then grinding leetcode. Yeah he is right no one is coming up with this solution if he hasn't seen this problem before hand

    • @nomad_1997
      @nomad_1997 Před 2 měsíci +18

      You're either a genius, or you memorize it. Pick and choose. That's what I've learned grinding LC for the last two months.

  • @AlokRatnaparkhi
    @AlokRatnaparkhi Před rokem +166

    For those who are wondering about -2 on the Line 13, for more readability you can rewrite this as :
    int partitionY = half - (partitionX + 1) - 1;
    //PartitionX is 0 indexed so you need to add 1 to exactly know the size of partition 1. Then you subtract it from half to know the size of partition B. Once you do that extra -1 at the end because we want to calculate index of partitionY. Partition should be 0 indexed as we are dealing with arrays. Even if you calculate this, you get half - partitionX - 2 in the end.

    • @amitbansal7754
      @amitbansal7754 Před rokem +3

      Thanks. I was looking for this explanation

    • @LavanVivekanandasarma
      @LavanVivekanandasarma Před rokem

      Ahh that makes sense ty!

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

      Thanks champ!

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

      this is well explained!

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

      I used it by default in my code and I was shocked that he used it too because no one on youtube has used this strategy for solving this problem

  • @clintondannolfo714
    @clintondannolfo714 Před 2 lety +224

    I've asked candidates this question for years at a Big Co and never once had a candidate successfully answer using this approach (after many interviews). Only a few even attempted it (but it wouldn't pass tests due to errors) and afterwards if candidates suggested it I only talked about it briefly to see their understanding and suggested them to do an easier solution. I feel like if I ever was made to implement the binary search version of this during an interview I'd fail, it's not really intuitive at all and neither is it telling me if a candidate is good or bad. Plenty of good people won't be able to get this in an interview, the false negative rate will be astronomical.

    • @atreides4911
      @atreides4911 Před rokem +33

      is there a reason why you were asking this problem if it was so bad at detecting positive signals? were you told to do it?

    • @clintondannolfo714
      @clintondannolfo714 Před rokem +38

      @@atreides4911 because I wouldn't expect the fully optimal solution. It's used just as a point for discussing about the problem and working through it, and getting some kind of working code. That part is where you get the signal, the signal is not from the solution itself. For myself anyway.

    • @atreides4911
      @atreides4911 Před rokem +5

      @@clintondannolfo714 I see, thank you, that's helpful!

    • @draganostojic6297
      @draganostojic6297 Před rokem +4

      It's hard LC and these are rarely asked in interviews

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

      But i did :)

  • @binetlee2534
    @binetlee2534 Před 3 lety +124

    Love your style of explanation, I was familiar with the log nature of binary search, but I couldn't wrap my head around the logic of what elements to choose each time. Your drawing of the partitions in different colors made a lot of sense, so thank you!

  • @Dhruvbala
    @Dhruvbala Před 10 měsíci +11

    I wish you'd picked two example arrays where one wasn't contained within the other. Would have made things a bit more clear

  • @ankitasinha3727
    @ankitasinha3727 Před 10 měsíci +18

    For people solving it in Java, replace line#12 with int i = Math.floorDiv(l+r, 2);
    As mentioned, Python integer division // behaves different than languages like C/C++ and many others. In Python, if you type -1//2 you get -1 whereas in other languages you'd get 0

    • @benpurcell591
      @benpurcell591 Před 6 dny

      Yes! Was wondering what on earth I did wrong when this dawned on me. Python was made for this question

  • @srinadhp
    @srinadhp Před 2 lety +49

    your videos are like watching an engrossing edge of the seat thrillers.. The way you uncover the solution and the clean code are unparallell. Thank you!

  • @lostdollars9945
    @lostdollars9945 Před 2 lety +9

    i really really appreciate how you humbly admit when a problem is hard even for you it gives so me much relief that ok maybe i'm not exceptionally stupid if a guy from google thinks it's a tough problem

  • @m_elhosseiny
    @m_elhosseiny Před 11 měsíci +87

    Thanks!
    Just a small note, can you give more difficult examples when running through the algorithm drawing?
    I feel it makes it validates your solution more rather than the ideal case which sometimes makes the viewer doubt wehter this would work with edge cases.
    for eg:
    you used 2 sorted arrays starting from 1 increasing by 1: [1,2,3..etc] with one smaller than the other in length.
    I would've loved a second example were you use another set of arrays.

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

      As far as I'm concerned he shows the direction in which to think, and there can be so many cases and that will make for a great video. I think it is better that the video is not too big and shows the main point of the problem

  • @anujsinha6467
    @anujsinha6467 Před 2 lety +10

    Hats off man, I didn't felt confused for a single moment with formula or any this else, just dry ran the whole cases and every thing makes sense. Thankyou!

  • @ax5344
    @ax5344 Před 3 lety +27

    whenever I tried to search for a problem and i saw your video--- although it is not on top---it feels like such a relief! Thank you, please do more!

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

    This is such a doozy problem !!! You have made my day with your clear cut explanation. I actually am understanding things which I thought I was too stupid to get !

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

    Thank you for explaining the core concepts of the algorithm - this was super helpful

  • @WaldoTheWombat
    @WaldoTheWombat Před rokem +8

    When doing a regular binary search on a single array, we cut the array in half each time to zero in on the item we search for. The same principle is implemented here, both of the arrays are sorted, so it make sense to check the middle item of each of them each and then searching in the correct part instead of just iterating from start to finish.

  • @yashjoon3889
    @yashjoon3889 Před rokem +8

    If anyone coding this problem in c++ , remember that in python -1/2 gives -1 whereas in cpp -1/2 gives 0. So apart from coding the above code similarly in cpp add this check before assigning a value to i :
    int i;
    if(l+r < 0)
    i = -1;
    else
    i = (l+r)/2;

    • @zachlin7192
      @zachlin7192 Před rokem +1

      thanks, just what im searching for!

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

      Great point. In typescript, this works similarly. So, you should write:
      const i = Math.floor((l + r) / 2)

  • @roman_mf
    @roman_mf Před rokem +20

    Took me a couple of re-watches to finally get that 'click' to understand your way of thinking. Amazing solution as always. I would say it's less about binary search itself and more about the logical thinking: we don't care about how the array will be partitioned, but we do care about rightmost value of left partition and leftmost value of the right partition. Knowing these values we can easily figure out the median value. Just brilliant. No way I would have ever came up to this solution by myself, let alone come up with this solution during the interview!

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

      Only a couple re-watches? You're super smart

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

    After watching this too many times to count, I finally understand some of the issues I have been having such as understanding how partitionY’s index is determined. You have no idea how impactful your videos are! Thanks so much ❤

  • @JackTheSpades
    @JackTheSpades Před rokem +30

    Important note for those trying to solve this in different languages. Pyhton integer division // behaves different than languages like C/C++ and many others. In Python, if you type -1//2 you get -1 whereas in other languages you'd get 0.

    • @PippyPappyPatterson
      @PippyPappyPatterson Před rokem +1

      Yeah, `//` is the floor division operator, not integer division.

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

      You are a lifesaver, man. I was literally struggling to understand how it would work if the division keeps giving 0 instead of -1.

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

      Yeah it was bugging me a lot, thanks man

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

      Yea man you saved 3 hrs of my life.

    • @VinitRaj-ym4lm
      @VinitRaj-ym4lm Před 6 měsíci +1

      yeah, we can use this function in java "Math.floorDiv(i+j, 2);"

  • @ianokay
    @ianokay Před 8 měsíci +2

    I wish he elaborated on 17:03 where he states "Now any of these indices could technically be out of bounds", because it seems odd in a binary search for the middle to be OOB, but I get that his while loop is not doing "while l

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

      The code actually fails if you do L R and the middle index becomes negative... but he didn't explain why exactly we need to do while True instead of L

  • @symbol767
    @symbol767 Před 2 lety +10

    I swear, if an interviewer asked me for the binary search method of this problem I'd be tempted to slap them.
    The binary search method is beyond unintuitive, like if you never seen this problem, it is very very very unlikely you would come up with this on your own, especially in 45min

  • @lovuitchen9332
    @lovuitchen9332 Před 3 lety +125

    Enjoy your videos, high quality and pythonic. Thank you! Just one note, it seems a "mistake" during explanation, at around 12:26-13:00, where it should be max(3,3)+min(4,4)/2, right? Because you wanna upper bound for the left arrays and lower bound for the right array, I believe.(which is correct in the code later actually--around 20:29)

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

      Thanks, I thought nobody else noticed

    • @SreenathV
      @SreenathV Před 2 lety

      (max(3,3)+min(4,4))/2

    • @rohitkumaram
      @rohitkumaram Před 2 lety

      @@toekneema I also noticed , but during my second watch.

    • @xuxiangyu7025
      @xuxiangyu7025 Před 2 lety

      @@toekneema I noticed

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

      I was totally searching for this comment. This should logically be the correct method for computing the median. The like from neetcode also confirms it, so that's a relief.

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

    Thank you, this problem was bane of my existence for a long time

  • @sankalparora9374
    @sankalparora9374 Před rokem +1

    By far the best explanation for Median of Sorted Arrays I ever got!
    Great job!
    Keep up!

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

    Your explanation is so clear! Thank you!

  • @jingwenren8157
    @jingwenren8157 Před 2 lety

    Very clear explanation! You've made my day!!

  • @georgeshaoyundong4590
    @georgeshaoyundong4590 Před 2 lety +17

    In c++, you have to use floor((l + r) / 2.0), otherwise you will not pass [1,3][2] test

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

      yep, python // is specifically floor division. That caught me off guard when translating this solution to another language.

    • @optimusprime5040
      @optimusprime5040 Před rokem +1

      Thanks, been scratching my head on it for an hour.

    • @sarthaksuper284
      @sarthaksuper284 Před rokem +1

      Thnx man

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

    Greatest explanation ever! Clean n clear

  • @yohguy4655
    @yohguy4655 Před 3 lety

    great explanation, thanks for going through examples.

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

    very clear explanation !! Thanks a lot !

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

    This is the best explanation (including the official leecode explanation) I could find.
    1. The most thing I hate about binary search is to set the ending condition, such as while l

  • @shashikantdivekar7839
    @shashikantdivekar7839 Před 3 lety

    Very clear explanation. Very helpful. Thank you Sir.

  • @mishooolica
    @mishooolica Před 2 lety

    Excellent explanation! Thank you so much!

  • @IntelliStar_
    @IntelliStar_ Před rokem +3

    I used to hate this problem and kinda avoid solving it. But because of your clear explanation, I finally can solve it. Many thanks!

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

      It's an extremely unintuitive system using a binary search to try to randomly shift around the first partition until it isn't clearly incorrect. It's strange to even START with the premise "Okay so our left (merged) half is the left half of the smallest sorted plus the first rest of the longest sorted, and work from that, as it makes no sense why that would even work, and obviously if the smaller one is all larger numbers, it illustrates the nonsense, but the algorithm eventually works it out. It's just a strange pattern of thought since it starts with a premise that is immediately clear in making no sense and being incorrect, and then thrash around (semi efficiently) until a sensical solution pops out.

  • @sauravus
    @sauravus Před rokem

    Excellent! You're a LIFE SAVER

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

    Does anyone know why you have to run binary search on the smaller array? I've tested it on the larger array and it fails on some instances.

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

    very clean code and well-explained! Thank you

  • @rongrongmiao3018
    @rongrongmiao3018 Před 2 lety

    Thanks Neet! Keep em coming

  • @shinoharakenshin7496
    @shinoharakenshin7496 Před 6 měsíci +2

    Just in case anyone else is stuck on trying to replicate NeetCode's code solution, for line #12 "i = (l + r) // 2" my replicated code was not able to pass submission but I managed to get all tests passed after I changed it to "i = (l + r) // 2 + l", the addition of l allowed me to properly use i as an array index to obtain the end of the first partition, without adding l its index would be offset from start of the entire array instead of from the actual starting position as marked by the l pointer. I also added the line "if (r-l < 0) i = -1;" under #12 to pass some edge cases. I don't know if this discrepancy is caused by misunderstanding on my part or if there was a mistake in the video, so I'm putting it up here if anyone knows.

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

    Super intuitive explanation, thank you NeetCode!

  • @scuderia6272
    @scuderia6272 Před rokem

    Finally an explanation that I was able to understand. Thanks legend 🙌🏻

  • @theghostwhowalk
    @theghostwhowalk Před rokem +1

    Wow one of the toughest questions made look ridiculously simple. Lot of edge cases. Missing your videos past 2-3 months.

  • @urunovtimes5816
    @urunovtimes5816 Před 2 lety

    I really like your approach style, keep going together

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

    Thanks for explanation, was asked this question this week and bollocks; it's too harsh for a phone interview despite it's not being a FAANG company!

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

    I comprehended about 80% of that so thank you! I'll rest my brain, probably rewatch, and hope for a 100% tomorrow.

  • @TheGoya
    @TheGoya Před 2 lety +11

    The part that's tricky to understand is where you increase/decrease the partition by 1, and why that doesn't lead to a degenerate linear case.

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

      yeah I also got the same question.. why this operation would not lead the time complexity to O(N/2) as only half the length at inital stage

    • @tacticisacting3702
      @tacticisacting3702 Před 2 lety +11

      that's not the partition being decreased by one, you have l and r values and i is in the middle of them, by setting r to i -1 or l to i+1 you halve the distance between l and r at each step so you get logarithmic runtime

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

    tbh the best part of the code is switching to process the shorter array between nums1 and nums2
    otherwise there is a lot of headaches when you move the pointer in an array that is much larger than the other

  • @Jess-xx9ul
    @Jess-xx9ul Před 8 měsíci

    this is so clear and understandable, thank you!!!! :)

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

    Clearest explanation ever, thanks you so much!

  • @pranavsharma7479
    @pranavsharma7479 Před 2 lety

    the best sol i found for this prob, thanks brother

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

    An edge case which I had expected to fail actually works correctly because the 'integer' (actually floor, not integer) division operator surprised me. With the shorter array having all values greater than the longer array, you end up with an i value of -1. I had expected -1 // 2 to be zero, rounding toward the origin, producing a wrong j index for the median, but I was wrong. The // operator always rounds down, even when dealing with negative results. In this case, -1//2 produces -1, so my fringe case still works perfectly.

  • @albertgao7256
    @albertgao7256 Před 3 lety

    For this puzzle, this is probably the clearest explanation online....

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

    I like your video so much, really appreciate it!!! Do you have a sheet summarizing all those 273 videos as you did before for the 75? Like, including difficulty level, problem title, and category.

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

    Thanks for the understandable explanation.

  • @srujanwankhede5314
    @srujanwankhede5314 Před 2 lety

    What a clean code .. thanks man

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

    Thank you for the video! I do have one question: at around 17:24, you said that in an edge case, i could be out of bound as i

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

      I have the same concern. I think `l` should start from -1 rather than 0. The algorithm never make the `l` variable decrease. So if the entire A array should go to the right part of the total array, there will be still at least one element(the A[0]) being put in the left part of the total array, which is wrong. The video is still very helpful though; I appreciate it a lot.

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

      Think about the case where A = [] and B = [1]

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

    Very Clear Explanation..
    Thank you so much

  • @yuurishibuya4797
    @yuurishibuya4797 Před rokem +4

    If ppl have not solved this before, it’s very unlikely that they can think of an optimum solution in the interview and code it covering all corner cases under 40+ minutes. Remember candidates have questions about team, work culture, etc as well to ask.
    I don’t know what knowledge about the candidate the interviewers will gain by asking these kind of questions.
    I have conducted a few hundred interviews, most of my questions tend to focus around what is expected to be known to work in my team from day one.

    • @TypicalRussianGuy
      @TypicalRussianGuy Před rokem +1

      Hello, what your questions tend to contain? Asking to write a solution to a typical work-related issue? Please explain if possible.
      Also, by the way, these interview questions are not really designed to test a candidate. They are mostly designed to make it as hard as possible for a candiate so that a company could deny as many candidates as quickly as possible. It's kinda cruel, but it's one of the only ways companies like Google and Amazon can deal with the huge influx of people applying for a position.

  • @davidlohmann5098
    @davidlohmann5098 Před 7 měsíci +1

    Could the same technique be used to find the median of 3 or more arrays of length n1, n2, n3, ... in O(log(n1 + n2 + n3 + ...)) time?

  • @RahulGupta_Grahul
    @RahulGupta_Grahul Před rokem

    Thank you. Very well explained!

  • @JS-wh7be
    @JS-wh7be Před rokem

    Clear explanation, helped a lot!

  • @jataman123
    @jataman123 Před rokem

    This explanation is so clear!

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

    Love your answer, best I have seen so far. There are many others using recursion which make it really hard to understand. Yours is a variation of standard while loop with left/right pointers binary search. And it's much easier to code this way.

    • @BarryBecker4
      @BarryBecker4 Před rokem

      The only problem is that it is not actually O(lg(n)). See comments above.

  • @yitongxie6574
    @yitongxie6574 Před rokem +1

    Thx for the infinity trick. Couldn't come up with it. I really scratched my head trying to solve the edge cases

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

    i was able to do this by myself but it took an entire day of brainstorming.

  • @fengliu975
    @fengliu975 Před 2 lety

    Still had to struggle bus for hours to get this but was able to attempt after watching first 2 min rather than having 0 idea like koko eats. These videos works man

  • @user-hh4kd4gp9m
    @user-hh4kd4gp9m Před rokem

    I tried a similar approach but it was much harder). Initially, I searched for the middle index in the first array and then binary search on the second array. Time complexity O(logNlogM). I didn't know how to improve until I found this one. This solution is genius. Well done!

  • @aravindantr4499
    @aravindantr4499 Před rokem

    Best Explanation. Thank you so much🤩🤩

  • @peanutbutter785
    @peanutbutter785 Před rokem

    This question is super hard for me. Thanks for the explanation!

  • @tomonkysinatree
    @tomonkysinatree Před 24 dny

    The way you write the solution out in code, it seems simple, but I am almost certain the edge cases with this approach would have totally stumped me if I get this in an interview

  • @matthewkellman1166
    @matthewkellman1166 Před 2 lety

    This was a fantastic explanation

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

    Thank you very much for all your solutions. They are really helping me a lot with your clear-cut explanations. For this question, I'm wondering why we have to ensure var A holds the array with the lesser length.
    At first, I thought it was to ensure a smaller time complexity but I decided to test. I removed the code ensuring A was smaller and a test case failed. Can you please explain?
    Thanks❤

    • @Perrychen25
      @Perrychen25 Před rokem +6

      If you don't assign the A with the smaller one, then you may encounter the case:
      A: [2], B: []
      half: 0, i = 0, j= 0 - 0 -2 = -2
      which will lead to ALeft: 2, ARight: inf, BLeft: -inf, BRight: inf and return min(inf, inf)

  • @DanielSmith-uj7rr
    @DanielSmith-uj7rr Před 2 lety

    Thank you for the explanation! :-)

  • @kahhuanyap6313
    @kahhuanyap6313 Před rokem

    Thank you again NeetCode!

  • @ilona7051
    @ilona7051 Před rokem +1

    I think you could just as well write 'r = i + 1' in line 30 instead of 'l = i + 1' (since the only time l appears in the while loop is in line 12: i = (l + r) //2 ). I think it makes more sense to increment the right pointer by 1 than to touch the left pointer.

  • @MikhailVainarevich
    @MikhailVainarevich Před rokem +9

    Thank you for explanation, it is really great for understanding the logic. One thing I'm struggling to understand is why this method is O(log(n+m)). When the condition is not met, we increment the 1st half of smaller array by 1 and check again. In the worst case, we are going to do it n/2 times. Isn't it O(N) time complexity?

    • @slavaplek9875
      @slavaplek9875 Před rokem +3

      I have exactly the same question

    • @pinakadhara7650
      @pinakadhara7650 Před rokem +3

      We aren't actually "incrementing by 1" but moving the left pointer to "mid + 1". If you consider the smaller array's length as 11, mid = (0 + 11) // 2 = 5. If the condition doesn't match in this iteration, we move the "left pointer" to 6. Now mid = (6+11) // 2 = 8
      I am not able to understand the why we are doing this though.

  • @thetaomegatheta
    @thetaomegatheta Před rokem

    If either of the input lists is empty, and the other one consists of few enough elements, one seems to get out-of-range errors on assigning values to Aright and/or Bright due to the expression 'i+1 < len(A)' and the equivalent one for B always being true.
    Am I missing something?

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

    Can somebody please explain at what part the "binary search" is happening?
    In lines 28/30, the right/left index is de-/increased by 1. I'd expect some halfing of an interval at some point, if a binary search was performed.

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

      We're decreasing/increasing the r and l pointers by subtracting or adding 1 to i and i is the value obtained by halving the interval at line number 12 :)

    • @kai75785
      @kai75785 Před rokem +2

      I fully agree with you. I believe this is worst case O(m), not O(log(min(m,n)). I see only 1 divide-by-2 and then a linear search. The "j = half - i - 2" is also just a guess which in his example works out perfectly but how about with (1, 2, 3, 4, 5) and (5, 6, 7, 8, 9)? You have to slide along about m/2 nodes - so it is O(m).

  • @ceffstudio
    @ceffstudio Před rokem

    Amazing explanation!

  • @mohammadagha8857
    @mohammadagha8857 Před rokem

    Thank you for the great explanation. What tool do you use for drawing?

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

    Thank you so much for these videos.

  • @maa1dz1333q2eqER
    @maa1dz1333q2eqER Před 2 lety

    Good explanation, thanks!!!

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

    Thank you for your brilliant work🤗

  • @weiqianzhang981
    @weiqianzhang981 Před rokem +7

    Fantastic video. Only one question. For this algo under the worst case, you actually need to move the pointer one by one till the end of an array and that may cause the time complexity to be O(m + n)? Because in order to get the log, isn't it necessary to move the pointer by half each time (not minus or add 1) so you can get the log part.

  • @rushilsharma762
    @rushilsharma762 Před 3 lety

    You're a life Saver

  • @anybody413
    @anybody413 Před 2 lety

    Superb teaching!!!

  • @aritralahiri8321
    @aritralahiri8321 Před 3 lety

    Wonderful explanation !

  • @moveonvillain1080
    @moveonvillain1080 Před rokem +1

    Very important note:
    If you are trying to write the same logic in a different language keep in mind that Python's ' n//m ' does floor division meanwhile in languages like JAVA, C++ n/2 is Integer divison. So make sure to use a floor function on (l+r)/2.
    For JAVA
    int i = (int)Math.floor((l+r)/2.0);
    basically we find the double value of the division then it's floor and then convert it to int as the Math.floor() returns the value in double.
    This had me pulling my hair for an hour.

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

    The Code explanation is NEET 😀👍👍👍👍👍!!!

  • @HaritsElfahmi
    @HaritsElfahmi Před 2 lety

    crystal clear thank you!

  • @senwang8670
    @senwang8670 Před rokem

    super talent on explaining !

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

    Could someone explain why `-2` in `j = half - i -2`; I do not get it, why both array start from 0 so we need to minus 2, should not it just for 1 array so minus 1 (I know it will lead to a wrong answer)

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

      because (i + 1) + (j + 1) = half, due to 0-index

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

    Love the visuals

  • @riteshbajaj6
    @riteshbajaj6 Před rokem

    Amazing explanation. Thanks

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

    Can somebody explain why r's values equates to len(A) - 1 instead or len(A) similarly why is the value of j equal to (half - i - 2) ? Any links to the explanation would also be helpful...

    • @anukritibanerjee
      @anukritibanerjee Před rokem +1

      The reason for r's value len(A)-1 and not len(A) is because we considering the index. For a list A = [1, 2, 3], the index of 3 will be 2 i.e A[2] = 3 but len(A) = 3 which is out of bound since there is no value at A[3] and the reason for this behavior is because indexing starts at 0

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

    I love this explanation!!!!

  • @ianokay
    @ianokay Před 8 měsíci +2

    It's an extremely unintuitive system using a binary search to try to randomly shift around the first partition until it isn't clearly incorrect. It's strange to even START with the premise "Okay so our left (merged) half is the left half of the smallest sorted plus the first rest of the longest sorted", and work from that, as it makes no sense why that would even work, and obviously if the smaller one is all larger numbers, it illustrates the nonsense, but the algorithm eventually works it out. It's just a strange pattern of thought since it starts with a premise that is immediately clear in making no sense and being incorrect, and then thrash around (semi efficiently) until a sensical correct solution pops out.
    What is weirdly unmentioned in this video is just the clarifying statement "What we're going to do is see how much of the smaller array we can use in the first half, by making the wildly incorrect assumption that it's the first half of it"
    (and it could be added "and correct our (almost certain) failure by moving logN instead of N+1, where we might actually over-shoot the actual correct portion, on behalf of a reduced complexity for large sizes of N")

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

    Thanks for the brilliant solution.
    Why does while r>=l not work here? If we use that, what changes would we have to make to the code?

    • @sreenithisridharan6136
      @sreenithisridharan6136 Před rokem +1

      If you had the r>=l condition for the while loop, it would break out of the loop in the case when you don't take any elements from the smaller array as the left half, for example A=[0,1,2,3,4,5], B=[7,8,9]. So in such a case, the median will be at the index 'half' of the larger array if 'total' is odd, or the average of the values at indices 'half-1' and 'half' if 'total' is even.
      So you will have to write the code to check if total is odd or even and return accordingly after the while loop.
      While it's okay to do so, I personally feel that would be like a repetition in the code, as we are implicitly dealing with the above case when we set the out of bound values to -infinity and +infinity and run it as a while True loop

  • @denysshushpanov7630
    @denysshushpanov7630 Před rokem

    I solved this problem only thanks to your explanation, thank you very much

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

    Hello Neetcoder,
    I have a request, Could you please make a video on leetcode problem number 2387 (Median of a row wise sorted matrix). It would be helpful.
    By the way, your content is amazing and keep up doing this great work. Thank you for all of the efforts you made from all of the neetcode family members.