Median of Two sorted arrays | O(log(max(m,n))) Time complexity |

Sdílet
Vložit
  • čas přidán 28. 10. 2020
  • This is the video under the series of DATA STRUCTURE & ALGORITHM. We are going to solve Questions from GeeksforGeeks or leetcode Median of Two sorted arrays in O(log(max(m,n))) Time complexity and O(1) Space Complexity.
    Given two sorted arrays arr[] and brr[] of sizes N and M respectively. The task is to find the median of the two arrays when they get merged.
    Which is a very famous and Routine question asked in Interview. The question is from the Topics Data structure. A full easy concept in Hindi. This is Question is asked in Many companies like Google, Amazon, Oyo Rooms, Paytm, Samsung, Adobe, etc..
    We also Provide courses on Competitive Programming and Data structure. Please see our Full Playlist on our Channel.
    ----------------------------------------------------------------------------------------
    ► Homework Question: leetcode.com/problems/median-...
    ----------------------------------------------------------------------------------------
    ► Median of Two sorted arrays: leetcode.com/problems/median-...
    ► PDF of Median of Two sorted arrays: github.com/Prince-1501/Hello_...
    ► CODE of Median of Two sorted arrays: github.com/Prince-1501/Hello_...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ►Competitive Programming : • How to start Competiti...
    ►C++ Full Course : • L-01 || Introduction a...
    ►Algorithms : • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    Our Students Contacts Form:-
    Form link: docs.google.com/forms/d/e/1FA...
    ------------------------------------------------------------------------
    #interview_preparation #geeksforgeeks #Hindi

Komentáře • 125

  • @tanuagrawal9249
    @tanuagrawal9249 Před 3 lety +31

    Since morning I was trying very hard to understand this problem . Finally I got it. Thanks a lot

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

      Keep sharing buddy

    • @SubhamKumar-9009
      @SubhamKumar-9009 Před 3 lety

      same

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

      Why we take end1 = n1 and not n1 - 1 because we are taking begin1 = 0 as from 0 to n1 there are n + 1 element and in all searching problem we take low as 0 and high as n - 1?

  • @jayeshyadav07
    @jayeshyadav07 Před 3 lety +29

    Pls make a video playlist of dsa 450 questions by love Babbar.

  • @tejasjaiswal1658
    @tejasjaiswal1658 Před 3 lety +18

    You have literally put your everything in the video. Great Content.

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

      Keep sharing with your friends ❤️

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

      @@HelloWorldbyprince Why we take end1 = n1 and not n1 - 1 because we are taking begin1 = 0 as from 0 to n1 there are n + 1 element and in all searching problem we take low as 0 and high as n - 1?

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

    Very good explanation. I would say the best explanation for solving "median of two sorted array" in CZcams. But I tried running the java version of this in leetcode and its showing "99.69 % faster than other Java submission".

  • @suhailmirza9609
    @suhailmirza9609 Před 3 lety

    You explained just like a friend . Thanks

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

    thanks sir! Really insightful

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

    This is the best explanation I have seen so far Thanks a lot

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

    excellent stuff. best video on this problem. I wanted to understand the intuition behind : i2 = (n + m + 1)/ 2 - i1. Why can't we just do i2 = (m-1) - i1, where m-1 is the index of the last element of the array2? This is assuming i1 = begin1 + (end1 -begin1)/2. At the start, end1 points to the last index of array 1 (n -1 ).

  • @abhigyansharma9108
    @abhigyansharma9108 Před 3 lety

    Bhai best of alll the videos i have seen in this question i saw ...

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

    Is video ke liye bahut bahut aabhar! Maza aa gaya jis prakar se aap bilkul zero level se batana shuru karte ho. Thanks a lot.

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

    This video is the best one I found to solve this question. Keep up the good work.Thankyou!!

  • @ankushladani496
    @ankushladani496 Před rokem

    Thank You bhaiya....
    Bahut badiya samjaya ek aur dekhna padega....

  • @saptarshimukhopadhyay6169

    man thanks for this video... you really made it look simple by the end...

  • @SharvanKumar-ui1kw
    @SharvanKumar-ui1kw Před 3 lety +4

    Thank u sir for it
    Plz continue👌

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

    Jo question aap karte hai samagh aata hai but jab apne se karne ke kosis karta hu to nahi hota hai

    • @exodus5948
      @exodus5948 Před 3 lety

      No problem Just listen the video carefully. Then try to solve the same question. you may or may not be able to solve it. but you will do atleast something. Here comes muscle memory. After seeing the code you should know which line is doing what.

  • @AyushMishra-kq8sm
    @AyushMishra-kq8sm Před 10 měsíci

    One of the best solutions. Thanks a lot for describing it in such a nice way.

  • @vipinpurohit2155
    @vipinpurohit2155 Před 3 lety

    Great video. I have two question. 1. why move left in case max1 > min2, why not right shift. 2. why left array should be small. would be great if you can help me out.

  • @raj1307
    @raj1307 Před 3 lety

    Great Explanation!

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

    Amazing, and thanks for creating it in Hindi

  • @srujanwankhede5314
    @srujanwankhede5314 Před 2 lety

    same code ab nhi chalta gfg pe IDK why unhone testcases change kiya lagta
    like {1,2,3,4,5} and {6,7,8,9,10,11} ka o/p 9 ata hai instead of 6

  • @ayushgoyal9871
    @ayushgoyal9871 Před rokem

    the best explanation for this problem

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

      Why we take end1 = n1 and not n1 - 1 because we are taking begin1 = 0 as from 0 to n1 there are n + 1 element and in all searching problem we take low as 0 and high as n - 1?

  • @codetogether24x7
    @codetogether24x7 Před 3 lety

    I had a doubt that why do you apply binary search on smaller size array??
    Is it because for smaller size array time complexity will be O(log(n)) and for larger size array it will be O(log(m) ??
    Please rply

  • @pankajpatil5077
    @pankajpatil5077 Před 3 lety

    good.explanation sir 👍👍

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

    Thank You Sir

  • @smkssksksm130
    @smkssksksm130 Před rokem

    Man you made it so easy for me thanks alot

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +1

      Thanks a ton bro Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @beinghappy9223
    @beinghappy9223 Před 2 lety

    One of the best explanation

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

    Well explained. Thanks!

  • @511-neelbutani9
    @511-neelbutani9 Před 3 lety +1

    Divinding i2 we choosed (n+m+1)/2-i1
    Can we divide it like n2/2+ 1?

  • @satishagrawal5015
    @satishagrawal5015 Před 6 měsíci

    thanks 🙏

  • @vishnuvardhan-md1ux
    @vishnuvardhan-md1ux Před 3 lety +1

    best solution so far.

  • @someone-zi8xc
    @someone-zi8xc Před 3 lety +2

    nahi bhaiyaa... achaa laga

  • @namansharma1448
    @namansharma1448 Před 3 lety

    why did u take end1 =n
    not (n-1)??
    can someone explain;??

  • @poorvaparande5933
    @poorvaparande5933 Před 2 lety

    Great video and super clear explanation! Can you please tell how is the time complexity O(log(m+n))? and why do we need to find i1 and i2 for the shorter array?

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

    Sir could u please 🙇🙇🙇explain the code for this input
    7 4
    -8 2 14 17 61 93 99
    -4 -3 -2 1
    while calculating manually I'm getting 2 as answer. as (-8,-4,-3,-2,1,2,14,17,61,93,99)median=A[11/2] = A[5] = 2.
    While executing the code I'm
    Getting -8(if I'm considering mid as (l+h)/2 or lower mid(l+(h-l)/2)
    I'm getting correct answer if I'm considering upper mid which is l+(h-l+1)/2.
    Could u please tell me sir generally in which case we have to use lower mid,mid ,upper mid.
    For few other test cases upper bound is not working sir.
    Please clarify my doubts sir,plzzzzzzzz.

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

    Why we take end1 = n1 and not n1 - 1 because we are taking begin1 = 0 as from 0 to n1 there are n + 1 element and in all searching problem we take low as 0 and high as n - 1?

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

    great explaination

  • @SHAUMAYAOJHABCE
    @SHAUMAYAOJHABCE Před 3 lety

    best solution explanation

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

    beautiful explanation❤️

  • @allmighty2000
    @allmighty2000 Před 3 lety

    this problem is next to impossible to think as a beginner ,

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

    thank you so much sir

  • @shalinikar8553
    @shalinikar8553 Před rokem +1

    Nice explanation. Thanks

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

    Best explanation lots of love bhai

  • @debajyotideba5001
    @debajyotideba5001 Před 3 lety

    just op 👍👍👍✔✔✔✔✔✔✔✔✔✔✔✔

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak Před rokem

    Nice bhaiya 😃😃

  • @SHAUMAYAOJHABCE
    @SHAUMAYAOJHABCE Před 3 lety

    can you explain the question "find factorial of a large number also?"

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

    Epic explanation bro🔥🔥🔥 please make videos on cp as well, i'd love to learn from u🙏

  • @vatsvlogs6110
    @vatsvlogs6110 Před rokem +1

    Hard one

  • @rajshah9129
    @rajshah9129 Před 3 lety

    Why we are taking max of max1,max2 and same for min can anyone explain??

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

    great explanation .......

  • @utpalpodder-pk6vq
    @utpalpodder-pk6vq Před 3 lety

    can you please mention the reason behind stressing more power on considering N

    • @rajneeshtyagi4894
      @rajneeshtyagi4894 Před 3 lety

      I also have same doubt,pls explain if you have the ans to it

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

      Because you want to get the time complexity O(log(min(m,n))) otherwise it will be O(log(max(m,n)))

    • @rajneeshtyagi4894
      @rajneeshtyagi4894 Před 3 lety

      Also you will not be able to divide the elements in two equal.sets if you calculate mid in the larger array

  • @abhijitbarik6669
    @abhijitbarik6669 Před 3 lety

    sir" Allocate the minimum no of pages" pe video banado please sir.#gfg

  • @KuldeepSahu-sq3cq
    @KuldeepSahu-sq3cq Před 3 lety +1

    please make video on matrix median.🙏

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 3 lety

      See this playlist :
      Matrix (Multidimensional Array) Complete guide For Interviews DSA: czcams.com/play/PLzjZaW71kMwRff0CCcrB93srEiQhJoOzg.html

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

    Bhai maza agya.

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

    Well explained!

  • @NotOmnipotent69
    @NotOmnipotent69 Před rokem

    if n=5 m=8 then why i2 = (n+m+1)/2

  • @jatinbhatoya8420
    @jatinbhatoya8420 Před 3 lety

    isme aise nhi krr skte jaise first arrays ke liye median nikaal lo. uske baad second array ke liye median nikaal lo and then uske baad unn dono ko add krke divide krr do 2 se

    • @ankitdubey9310
      @ankitdubey9310 Před 3 lety

      doosra array bahut bade number of elements ka ho sakta hai, to jo net array banega usme doosre array ka domination hoga naki unke average ka

  • @035asadali8
    @035asadali8 Před 2 lety +3

    i done this in few lines but time complexity not good and i use vector function
    class Solution {
    public:
    double findMedianSortedArrays(vector& nums1, vector& nums2) {
    vector v;
    v.insert(v.begin(), nums1.begin(), nums1.end());
    v.insert(v.end(), nums2.begin(), nums2.end());
    sort(v.begin(),v.end());
    int n=v.size();

    float sum;
    if(n%2==0){
    sum=(v.at(n/2-1)+v.at(n/2))/2;

    return sum;
    }else{
    sum=(v.at(n/2)+v.at(n/2))/2;
    return sum;
    }
    }

    };

  • @035asadali8
    @035asadali8 Před 2 lety +1

    i solved this ,but using vector functions

  • @nainseevishwakarma8692

    Aap samja achha rhe ho per pliz pent me line by line samjhao to or achhe se clear hoga

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

    This code fails for the input case [3,4]
    [1,2]

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

    nice sir

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

    bhai achha smjha rha hai but ye pdhate pdhate screen ke bhich me apna insta id na dikhao jise follow krna hoga vo kr hi dega , pda rhe hai ki advertisement kr rhe hai !!1

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

    bro agar tum marker use karke bta dete to or achha hota

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

      Yup ...but mere pass marker and white board ka setup nahi hai 🙄

  • @rudra4830
    @rudra4830 Před 3 lety

    same solution is discuused by sandeep jain gfg
    edit:and examples taken are also same

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

    why (m+n+1/2)? i did not understand this?

    • @shuchitam3868
      @shuchitam3868 Před 3 lety

      Also the time complexity according to me should be O(lg (min(m,n)) -- because you are taking min length arr extremes and calculating median

  • @ShuvamGhosh-eq9xd
    @ShuvamGhosh-eq9xd Před rokem

    s+(e-s)/2=m isse ni yoga kia

  • @rishiSingh0898
    @rishiSingh0898 Před rokem

    smagh me nahi ara ha "Efficient solution se "

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      kyu

    • @rishiSingh0898
      @rishiSingh0898 Před rokem

      @@HelloWorldbyprince jaha se optimised approach shuru hui waha se ki dono array kase divide hore ha mid ke liye wo smgh me nahi aya,
      I will watch it again once

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

    Please try to use some notepad on your computer and record it while solving problems. You're approach is amazing but it might be more helpful if we could see you solve in front of us rather than us trying to imagine stuff or explicitly solve it parallelly when you explain it, while pausing and playing the video. The zoomed slides and screenshots are kind of irritating and does not let me focus on where you are. Need to actively watch you and see at what point we are. Hope you understand. Thanks for the amazing approaches and solution though! :)

  • @therishabhdhiman
    @therishabhdhiman Před rokem +1

    A much more simpler solution.
    The approach which I'm following is imagine we are going to merge the array into a completely new array. Then itearate the array in that way checking the value which we should put next. In this way if the array is off break the loop after half cycle and return the element at current position and if the number is even then break the loop after (n+1)/2 and sum nth and nth+1 element and divide by 2 and return.
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    total = nums1.length + nums2.length;
    even = (total % 2 == 0);
    int left = 0, right = 0;
    for (int z = 1; z

  • @VikashSingh-kg8xr
    @VikashSingh-kg8xr Před 3 lety

    Aap samjhaye shi ho lekin ek hi word 50 baar bol kr irritate kr diye ho . Aisa lag rha ki
    Aap khud hi confuse ho .
    Maine sujhav diya hai baki aapki marzi .
    Hme lgta hai ye ko aap 10 min me bata dete ager aap ke under confidence hota toh .

  • @didyouknowthisweird
    @didyouknowthisweird Před rokem

    Title : English , Video Language: Not English

  • @Anuj-vf7bg
    @Anuj-vf7bg Před rokem

    👎👎👎👎👎

  • @chetansengar9833
    @chetansengar9833 Před rokem

    For those, who are looking for //Base Case. Please find below.
    double findMedianSortedArrays(vector& nums1, vector& nums2) {
    int n = nums1.size(); int m = nums2.size();
    if(n>m) return findMedianSortedArrays(nums2,nums1);
    if(n==0) return m%2 ? nums2[m/2] : (nums2[m/2]+nums2[m/2-1])/2.0;
    if(m==0) return n%2 ? nums1[n/2] : (nums1[n/2]+nums1[n/2-1])/2.0;
    int begin1=0, end1=n;
    while(begin1

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

      Why we take end1 = n1 and not n1 - 1 because we are taking begin1 = 0 as from 0 to n1 there are n + 1 element and in all searching problem we take low as 0 and high as n - 1?