Kth Smallest Element in a Sorted Matrix | Algorithm Explanation by alGOds

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

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

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

    Join this telegram channel for regular updates : t.me/alGOdsRUclips
    Join this telegram channel for doubts and discussions : t.me/joinchat/PqjeMhz0MBR-tg31OK2Dsg

  • @JaiPrakash-ku7it
    @JaiPrakash-ku7it 4 года назад +12

    I didn't get it. It is too difficult to get for a beginner. Please explain its code if possible.

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

    very nice explaination i am struggling with this quetion since a week but we can also use upperbound function to calculate the number of elements which are smaller than k by iterating over rows

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

    Thank you for the video. Just some minor comment: When discussing complexity, I'd always simplify the classes just to avoid confusion when you are comparing different algorithms with similar complexities. For example, at 2:06, I'd rather say O(n^2 log n).

  • @Sunny-ri4yo
    @Sunny-ri4yo 4 года назад +3

    Hey! how will this solution handle the case when suppose mid is 4 (4 isn't in the matrix) and number of elements less than 4 comes out to be required then we have to decrease the high bound, but what if answer was actually 5 (which was actually present in the matrix) but we will never check for 5.

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

      Same Doubt

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

      In that case, we won't change the low variable but assign mid-value to the high and we go for another iteration to check the same count is occurring or not if it does then the previous count value is not the correct one!

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

      you can go to this link for a better explanation ruclips.net/video/dpsp1hP6P-U/видео.html

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

    Thanks bro for this intuition. Saved me a lot of time.

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

    (low+high)/2 is subject to overflow. Use low + (high-low)/2 OR high - (high-low)/2

  • @yy-qx9lx
    @yy-qx9lx 4 года назад +3

    bro from where did you learn this algorithm?

    • @alGOds99
      @alGOds99  4 года назад +4

      This algorithm is a mixture of two algos.
      There's no exact source available to learn this algorithm.
      It comes from practice and intuition :)

    • @donnsharma9490
      @donnsharma9490 4 года назад +4

      Educative.io course grokking the coding the interview is the source or go to the Leetcode discuss there you will get nicer explanation of this question

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

      ruclips.net/video/oeQlc87HbbQ/видео.html

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

      he copied it from leetcode. if he did not copy he would explained the so called "intuition", but no, he jumps straight into algorithm which is a clear indication that he didn't come up with this on his own.

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

    At 2:10 why do we need to sort the array? why can't we just iterate the matrix with two loops and print the value at k-1. This will give us O(n^2) complexity right?

    • @Cube2deth
      @Cube2deth 4 года назад +4

      Maybe the matrix will have
      1 2 3
      4 5 8
      7 8 9
      See the 7 will come after 8 in your soluitino (the last element of previous row might no be bigger than first element of next row:D

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

    int solve(int mat[][c], int r, int c, int k){
    int min = mat[0][0], max = mat[c-1][c-1];
    int desired = k;
    while(min < max){
    int mid = min+(max-min)/2;
    int place = 0;
    for(int i=0; i

  • @pqazx1
    @pqazx1 4 года назад +4

    Plz solve using heaps as well..

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

    Why during dry run you applying binary search on the elements instead of the index of the elements, is there any specific reason?

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq 4 года назад +1

    Hi, Can you explain this(08:45) a bit more, please?

  • @SHASHIKUMAR-pp4hg
    @SHASHIKUMAR-pp4hg 3 года назад +7

    good for nothing,why you cannot simply return a[(k-1)/c][(k-1)%c]

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

      Doesn't work for the following matrix
      1 4 7
      2 5 8
      3 6 9
      Now for 4th element using your formula we get, a[(4-1)/3][(4-1)%3] = a[1][0] = 2. Which is obviously not the correct answer.

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

      @@satishmhetre5301 sorted matrix means 123 456 789.. otherwise the author would not have mentioned first method to push them in array and choose arr[k-1] out of that

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

      Matrix given by me is row wise sorted.

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

      @@satishmhetre7117 then this approach won't work

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

    Omg, You made it so simple... I was stuck with this for 3 days, solved it in 10 mins

  • @AmazingWorld-fw9oc
    @AmazingWorld-fw9oc 3 года назад

    Count function explanation is not good plzz give more details about it.

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

    you are really god of algos keep continuing this good work

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

    Need of count ??

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

    Wonderful explanation!

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

    Excellent 👌

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

    Keep it up. Going very well👍👍

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

      Thank You!!😁😄

  • @sharmaak8039
    @sharmaak8039 4 года назад +4

    Veryyyy well explained ✨🔥

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

      Thank You😁😇

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

    didnt understand

  • @AmazingWorld-fw9oc
    @AmazingWorld-fw9oc 3 года назад +3

    this tutorial was not helpful at all, please give a better explanation from next time.

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

    Thanks

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

    Awesome explanation!

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

      Glad you liked it :)

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

    Your approach is a bit wrong.

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

    It was difficult to understand but great work 😃

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

    Mere se bhi dislike le le bhai

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

    Very bad explanation although this solution is a good one...