BS-24. Search in a 2D Matrix - I | Binary Search of 2D

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

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

  • @takeUforward
    @takeUforward  Год назад +9

    Please watch our new video on the same topic: ruclips.net/video/JXU4Akft7yk/видео.html

  • @AbhishekSharma-ip4df
    @AbhishekSharma-ip4df Год назад +31

    bhaiya..kya approach..kya intuition..kya energy..explain krne ka tareeka..legit sb awsm hota h aapka..u make us feel dsa🔥🔥

  • @debanjanghosal618
    @debanjanghosal618 Месяц назад +6

    I solved this problem on my own using a slightly different approach:
    My intuition was since each row in the matrix is sorted in ascending order and the first element of each row is greater than the last element of the previous row, the matrix can be treated as a sorted 2D grid. Starting from the top-right corner is effective because:
    - If the current element is greater than the target, moving left in the row helps since all elements to the left are smaller.
    - If the current element is less than the target, moving down to the next row is logical, as all elements below will be larger.
    - If the current element is equal to the target, we return true.
    This approach efficiently narrows down the search space by using the properties of the sorted matrix. This approach's time complexity will be O(m+n), which is slightly worse than the optimal solution of O(log(m*n)).
    This approach can also be used to solve the problem of finding the row with max 1's.

    • @bhushanambhore8378
      @bhushanambhore8378 Месяц назад +2

      That's a nice approach. I solved it by this approach like, I applied binary search on row which eliminates unnecessary rows that doesnt contains target element, this results to a row which may or may not contain target element and applied again binary search on columns of resultant row which i eventually find out or may be not target elem is present.
      This take TC: O(Log M + Log N) which is equal to O(Log M*N)

  • @prachigoyal2510
    @prachigoyal2510 Год назад +17

    A huge thanks Striver for putting all these efforts 🙏

  • @princebhanushali6255
    @princebhanushali6255 6 месяцев назад +4

    Best DSA questions and approaches among all the DSA tutorials on RUclips. Big thankss❤❤

  • @KashafZia-d7p
    @KashafZia-d7p 3 месяца назад +1

    watched multiple videos of this question but only this video cleared my concept.Hats off

  • @muditsinghal6042
    @muditsinghal6042 3 месяца назад +2

    I did log m + log n basically the same, by doing bs first on first column and then on that row, it was more intuitive following your playlist, but i did think of this method but got stuck in how to convert 2D to 1D, loved the progess i made, thank you so much

  • @sairam3351
    @sairam3351 Год назад +8

    You are excellent teacher bro,no professor comes close, 💖

  • @harshhwardhanrai3716
    @harshhwardhanrai3716 5 месяцев назад +3

    at 10:15 I literally started searching for the intuition for it but thanks for explaining it :)

  • @Gagansharma3002
    @Gagansharma3002 10 месяцев назад +5

    Bhai babbar ke to bus hype h asli udham to tumne machai h student ke learning me❤

  • @chiragsharma8905
    @chiragsharma8905 Год назад +22

    My code with slightly more intuitive approach(according to me). O(log(n)+log(m)).
    Finding correct row first, and then finding target in that row.
    class Solution {
    public boolean searchMatrix(int[][] mat, int target) {
    int m = mat.length;
    if(m==0) return false;
    int lo = 0;
    int hi = m-1;
    while(lo

    • @saswatrath4646
      @saswatrath4646 7 месяцев назад +5

      great bhai same as my approach

    • @srujangajjala2658
      @srujangajjala2658 5 месяцев назад +14

      log n + log m = log nm so time complexity is still same

    • @RajNamdev_19
      @RajNamdev_19 4 месяца назад +4

      @@srujangajjala2658 thanks for reminding me log formula I just forgot about it

    • @muditsinghal6042
      @muditsinghal6042 3 месяца назад

      I did the same 😂😂

    • @chiragsharma8905
      @chiragsharma8905 3 месяца назад

      @@muditsinghal6042 nice

  • @jhanavikumari274
    @jhanavikumari274 7 месяцев назад +1

    you are definitely doing some sorcery out there like how can you make learning this interesting .

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

    Optimal approach.
    Public class solution {
    static boolean Search in matrix(
    Array list mat,int target){
    int m=mat.size(),n=mat.get(0).size ();
    int low=0, high =m*n -1;
    while (low

  • @rising_star17
    @rising_star17 2 месяца назад

    legendary explanation

  • @Gokul-gkl
    @Gokul-gkl 8 дней назад +1

    0:57 Apple ecosystem spotted 😮

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

    Understood! Super wonderful explanation as always, thank you very very much!!

  • @devkirandass7930
    @devkirandass7930 Год назад +30

    Striver is the best...Feel like worshipping him🥰

    • @SandeepM0ttan
      @SandeepM0ttan Год назад +3

      Try kunal kushwaha then

    • @devkirandass7930
      @devkirandass7930 Год назад +9

      @@SandeepM0ttanyeah I know him....idk but I felt like he is too arrogant ....and he teaches JAVA and I am into C++

    • @aspiring_Sr.dev_Om
      @aspiring_Sr.dev_Om 3 месяца назад +1

      @@SandeepM0ttan kunal kushwaha is on another level

    • @SandeepM0ttan
      @SandeepM0ttan 3 месяца назад

      @@devkirandass7930 gotta Admit He is a Little Arrogant but he teaches good

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

    I solved it by this approach like, I applied binary search on row which eliminates unnecessary rows that doesnt contains target element, this results to a row which may or may not contain target element and applied again binary search on columns of resultant row which i eventually find out or may be not target elem is present.
    This take TC: O(Log M + Log N) which is equal to O(Log M*N)

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

    Understood! Such a great explanation as always, thank you very much for your effort!!

  • @poonam-kamboj
    @poonam-kamboj Год назад

    very nice and clearly explained , thanks for the good explanation!!

  • @ShahNawaz-cx3pi
    @ShahNawaz-cx3pi 5 месяцев назад

    One more solution can be , first try to find out the correct row from 0th column (element in 0th column

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

    A great explanation as a whole bruh

  • @ankanmalik409
    @ankanmalik409 Год назад +3

    dada make a video on - Divide two integers without using multiplication, division and mod operator

  • @abhishek-u7c
    @abhishek-u7c 26 дней назад

    we can apply binary search on column=0 on all arrays to find greatest element less then target. which takes log(row) time.
    and apply binary search on that row which takes log(col) time. 🙂

    • @Gokul-gkl
      @Gokul-gkl 8 дней назад

      we only going to perform binary search only if the target is present on that row, so it always takes O(n*logm) which is the very worst case where all rows have the chance to have the target literally means the starting(first element on row) will be less than the target and the end(last element) will be greater than the target, which push all the row to get executed but having only one possible chance, in that case it will be O(n*logm) otherwise it will defenately going to be lesser than that

  • @mrminus8590
    @mrminus8590 5 месяцев назад

    Thanks for everything brother

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

    very very helpfull explaination

  • @AdityaAgrawal-fi7np
    @AdityaAgrawal-fi7np 3 месяца назад +2

    You are best ... agar mei eak devloper bana tho you too get the credit

  • @kaichang8186
    @kaichang8186 2 месяца назад

    understood, thanks for the great video

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

    understood 😇

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

    understood

  • @sayantanpoddar5428
    @sayantanpoddar5428 9 месяцев назад +1

    Understood.............Understood!!!!!
    G.O.A.T

  • @oyeshxrme
    @oyeshxrme 3 месяца назад

    understood bhaiya

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

    Thanks Bhaiya 💯❤

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 9 месяцев назад

    Thank you Bhaiya

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 9 месяцев назад

    done and dusted❤‍🔥❤‍🔥just awesome

  • @YourCodeVerse
    @YourCodeVerse 11 месяцев назад

    Understood✅🔥🔥

  • @md.imrankhan6912
    @md.imrankhan6912 Год назад

    Thank You Mr Legend

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

    amazing..thanku

  • @aradhyapandey1489
    @aradhyapandey1489 11 месяцев назад

    bhaiya...kitna bhayankar padhate ho🙏

  • @artifice_abhi
    @artifice_abhi 10 месяцев назад +1

    but its not necessary that if my target lies within the range it will surely be there for eg if an array is: 3 6 7 8 and target is 5 so now I would apply binary search as it is within my range but actually its not present.
    And this can happen for the rows where every time target is within the range but not present there.
    So in worst case time complexity would be: N*log(M)

    • @harshilpatel3205
      @harshilpatel3205 9 месяцев назад

      No brother time complexity in the worst case stays as 0(n) + 0(log2(m)) ... Because once we find the answer we are returning the answer so it'll never go through each row...

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

    Understoood✌✌👍👍

  • @iceyyeah
    @iceyyeah 10 месяцев назад

    You go crazy love that dude

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

    Understood, thank you.

  • @Ajeetkumar-uo4hf
    @Ajeetkumar-uo4hf Год назад

    Striver Energy OP !!

  • @graviton001
    @graviton001 4 месяца назад

    Solved myself ❤

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

    understood thanks

  • @KartikeyTT
    @KartikeyTT 4 месяца назад

    tysm sir

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

    Understood ❤

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

    Understood thank you

  • @pulamollavenkatreddy8162
    @pulamollavenkatreddy8162 Год назад +7

    Can we do this O( log(n)+ log(m)) by eliminating rows

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

      yes

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

      log(m) + log(n) = log(m*n) as per logarithms.. So both time complexities are same

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

    Understood...............

  • @piyushmahale9024
    @piyushmahale9024 5 месяцев назад +1

    ❤❤❤❤

  • @shashankgsharma0901
    @shashankgsharma0901 5 месяцев назад +1

    Understood, but please take m x n instead of n x m, took i / m the whole time taking m x n, so got segmentation fault the entire time.

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

    It can also be done in O(n).

  • @sanketatmaram
    @sanketatmaram 3 месяца назад

    understood!

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

    Hey Striver , I somehow find this question and Median in a row-wise sorted Matrix a bit similar , but my approach gets wrong when i try to flatten the matrix to find the median. Is my approach wrong towards the question?

  • @RituSingh-ne1mk
    @RituSingh-ne1mk 11 месяцев назад

    Understood!

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 10 месяцев назад

    Understood

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

    Which is lesser time complexity, log(m) + log(n) , or log(m*n) ?

    • @rohitsai806
      @rohitsai806 Год назад +4

      both are same... log a + log b = log ab

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

    understood

  • @kundann_n9989
    @kundann_n9989 4 месяца назад

    Can we not do a binary srach on i to look for the row that contains the target the do binary search on that row to look for target?

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

    Same code u submitted is giving me TLE error for me sir

  • @hareshnayak7302
    @hareshnayak7302 8 месяцев назад

    Understood,thanks striver for this amazing video.

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

    peak intitution

  • @ravitalaugh9017
    @ravitalaugh9017 2 месяца назад +1

    not working for [[1,1]]

  • @suryasaimaheswar8636
    @suryasaimaheswar8636 4 месяца назад

    understood :)

  • @ayushagarwal5271
    @ayushagarwal5271 5 месяцев назад

    coded it down myself! thanks!!

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

    Amazing solution

  • @manikantachegu2182
    @manikantachegu2182 4 месяца назад

    How about finding the row in which the element will be (by binary search ) and then applying binary search on the row can result in
    logn + logm crct me if im wrong because it worked for me in coding ninjas

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

    Please help Indians become candidate masters on codeforces.

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

    i solved this in O(log n + log m)

  • @shloksharma-p7y
    @shloksharma-p7y 2 месяца назад

    bhaiya aap to ye optimal tak soch lete ho meri to bruteforce pe hi soch ruk jati hai iska kya karu abhi ek mahina ho chala phir bhi bruteforce tak hi logic ban raha hai

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

    bhaiya why did you take log of base 2?

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

      search space is reducing by half everytime we are doing mid = (low +high)/2. Anytime if you are reducing your working space by diving something your time complexity becomes log base to diviser. Had we been doing mid = (low + high)/5, just imagine it, our time complexity would have been log base 5. Hope it helps!

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

      @@saswatrath4646 thanks for explaining:)
      But I can't grasp why it happens...why the time complexity becomes log of base of the divisor

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

      @@studyah Search space is reducing from N to N/2 to N/4 to N/8 and so on.. why so ? bcz we are doing eliminating one half either left of right from the mid according to our situation. before you read any further 2^k means 2 raised to the power k. Here is the detailed explanation buddy.
      You can also write N, N/2, N/4, N/8..... as N/2^0 , N/2^1, N/2^2, N/2^3.....let's say it goes till 2/2^K.
      N/2^K = 1 hoga right ? kyuki last me 1 element == target hoga. Solve the equation, N = 2^k hog. Recall the formula of log and exponential or search it on google, if N = 2 ^ K then K = log base 2 of N. Now if you observer we are going from 1 to K right ? when we were at 1 we considered the whole array, when we were at k we comsidered only a single element. So, we moved from 1, 2, 3,...K. K number of iterations, now what is K that we found out above ? log base 2 of N. Time complexity will be O(k) =O(log base 2 of N) .

  • @nakulchauhan6713
    @nakulchauhan6713 10 месяцев назад

    Your content and amount of effort is great.
    Huge respect for this course and you.
    But i feel you are not poking students to think, it appears as if you already know answer and here you are explaining it.
    Thought to process to come to a point should also be recorded.
    Lets say live solving DSA problems.

    • @Fe-ironman
      @Fe-ironman 10 месяцев назад

      You should watch this video only after going through the problems yourself

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

    BF approach.
    Public class {
    static boolean search matrix (Array list mat,int target){
    int m=mat.size(),
    n=mat.get(0).size ();
    for(int i=0;i

  • @aniketdas4358
    @aniketdas4358 9 месяцев назад +1

    why am i getting runtime error with this code
    bool find(vector &arr,int col,int target)
    {
    int lo=0;
    int hi=col-1;
    while(lotarget)
    {
    hi=mid-1;
    }
    else if(arr[mid]

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

      when u r asking out do mention type or name of the error, it wil assist the person trying to help u out.

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

      change col to mat[i].size() and row to mat.size(), and write the col inside the for loop before calling bool function it will work perfectly fine.

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

    UnderStood

  • @suryodhan3060
    @suryodhan3060 2 месяца назад

    actually you are keeping a lot of effort but unable to understand it 😭😭

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

    us

  • @PrabhaKaran-36
    @PrabhaKaran-36 7 месяцев назад +1

    Understood❤

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

    Understood!

  • @PrinceTripathi-j7u
    @PrinceTripathi-j7u 10 месяцев назад

    Understood

  • @KAMLESHSINGH-vr3bl
    @KAMLESHSINGH-vr3bl Год назад

    understood

  • @anshsaxena7297
    @anshsaxena7297 3 месяца назад

    UnderStood

  • @nayankhuman1043
    @nayankhuman1043 4 месяца назад

    Understood ❤

  • @chiragbansod8252
    @chiragbansod8252 8 месяцев назад

    understood

  • @ganeshvhatkar9253
    @ganeshvhatkar9253 10 месяцев назад

    Understood

  • @anshsaxena7297
    @anshsaxena7297 3 месяца назад

    UnderStood

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

    understood

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

    Understood

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

    understood

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

    Understood

  • @shashankvashishtha4454
    @shashankvashishtha4454 5 месяцев назад

    understood

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

    Understood

  • @RishabhKumar0094
    @RishabhKumar0094 3 месяца назад

    understood

  • @sandeshgupta7950
    @sandeshgupta7950 5 месяцев назад

    Understood

  • @RajNamdev_19
    @RajNamdev_19 4 месяца назад

    Understood

  • @yrzwisdom
    @yrzwisdom 4 месяца назад

    Understood

  • @krishkumar491
    @krishkumar491 3 месяца назад

    Understood

  • @SitaRam-m1i
    @SitaRam-m1i Месяц назад +1

    Understood