Print Matrix Diagonally (Diagonal order)

Поделиться
HTML-код
  • Опубликовано: 9 окт 2024
  • Print the matrix Diagonally. You have to print the matrix in diagonal order.

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

  • @Dadwalfamilyadventures
    @Dadwalfamilyadventures 7 лет назад +12

    While conditions should be while (i >= 0 && j

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

      Yes bro now working fine all test cases passed 🙌

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

    Thank you for walking us the process of how to solve this type of problems, I find it very helpful!

  • @shiyuwang
    @shiyuwang 6 лет назад +15

    Thank you so much! This is very clear, this question is my interview question today. I should have watched it earlier.

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

    Very nice explanation sir but there is a bug in your code(the condition of the while loop will be i>=0 && j

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

    By adding condition (i>=0 && j

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

      yes yes i also figured it out just now, this code is not complete

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

    very well explained, keep it up, thank you sir 👍👍

  • @Dadwalfamilyadventures
    @Dadwalfamilyadventures 7 лет назад +11

    Good job in explaining well. All your videos are great. Cheers!! Keep the good work

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

    Thanks for ur video. However, there is a bug in your nested while loop.
    consider a 4 by 2 matrix. Correct answer I think should be
    def dia_print(arr):
    output = []
    for i in range(arr.shape[0]):
    j = 0
    while i >= 0 and j

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

    You're awesome. I love your video explanations!

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

    The same program in python 3, thanks for your help.
    # python 3x example
    # diagonal iteration of array m x n.
    matrix = [ ['a', 'b', 'c', 'd', 'e'],
    ['f', 'g', 'h', 'i', 'j'],
    ['k', 'l', 'm', 'n', 'o'],
    ['p', 'q', 'r', 's', 't'] ]
    m = 4 # rows
    n = 5 # columns

    for k in range(0, m):
    i=k
    j=0
    while i >= 0:
    print(matrix[i][j], end = "")
    i = i-1
    j = j+1
    print()
    for k in range(1, n):
    i = m-1
    j = k
    while j

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

      Is this program for specific element pls help I need diagonal of specific element matrix 3*3 k=6 output=[2,6] and [6,8]

  • @Vishal-nc8ju
    @Vishal-nc8ju 5 лет назад +1

    if you are reading this comment then believe me guys this is the best channel to improve your thinking skills.
    It helps me a lot. thanku sir

  • @awalktoinfinity665
    @awalktoinfinity665 4 года назад +8

    hi sir ! actually in both while ..you missed one more condition i.e., to ensure to i and j both to remain within boundaries of matrix .. in the first while for printing the row side diagonal you should put a condition so that j should be within "n" . for ex take matrix of size 5*2 . check this program will not work and will throw array index out of bound exception .
    similarly for second while in the second for loop , put a condition for i>=0 .
    anyways great video

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

      Could u write it sir, cause I want today really.

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

      @@MrShortReels you are asking whom ?

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

      You commentator can u add the missing.. To algorithims

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

      Cause I want to convert this to matlab algorithim

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

      It does work. I did test it

  • @user-youyoki9054
    @user-youyoki9054 4 года назад +2

    I really want to push the LIKE👍🏻👍🏻👍🏻👍🏻👍🏻👍🏻 button ONE MILLION TIMES!!
    Thank you such a million, MASTER!!

    • @user-youyoki9054
      @user-youyoki9054 4 года назад +1

      void MakeDiagonalArray(int ** ar, int n1, int n2) {
      int i, j;
      int k, num = 1;
      /*
      The variable 'k'
      - trace through every starting of the diagonals
      The variable 'i', 'j'
      - go on incrementing i by +1
      - go on decrementing j by -1
      The variable 'num'
      - 각 배열에 자연수를 대입.
      */
      // Tracking first elements of the columns
      for (k = 0; k < n2; k++) {
      i = 0;
      j = k;
      while (j > -1 && i < n1) {
      /*
      Each diagonal ends at the first column
      or the last row.
      */
      ar[i][j] = num++;
      i++;
      j--;
      }// while (j > -1 || i < n1)
      }// for (k = 0; k < n2; k++)

      // Tracking last elements of the rows
      for (k = 1; k < n1; k++) {
      i = k;
      j = n2 - 1;
      while (j > -1 && i < n1) {
      /*
      Each diagonal ends at the first column
      or the last row.
      */
      ar[i][j] = num++;
      i++;
      j--;
      }// while (j > -1 && i < n1)
      }// for (k = 1; k < n1; k++)
      }// void MakeDiagonalArray(int ** ar, int n1, int n2)

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

    In the second while the condition i>=0 is necessary, otherwise it goes out of bounds.☺

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

    Finally have landed into the guru of algorithms bless you & namaskaram

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

    Amazingly helpful sir . Cleared my confused concept thanks a lot sir . Do make more videos like these...

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

    Very Well Explained Sir. SImple Explanation and Nice Illustration. I always refer to this video when i am stuck on Algorithms.
    THank You Sir Once Again.

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

    Can't we use the (i+j) and then compare it to the x variable with the help of IF function in a loop and then increment x.........??

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

    I should have watched this video earlier today. I was asked to solve this in my interview today!

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

    respect for you sir explained so easily

  • @foo.1396
    @foo.1396 6 лет назад +1

    I suppose the conditions of both while loops should be combined otherwise the program fails for 3x2 Matrix as it would eventually try to look for element(0,2) which is not present.

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

    The question is will these formulas work for n by n matrix?

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

    thank you sir, this solution is awesome 🙏

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

    Cool, but what If you want it to traverse it in the opposite way?

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

    Your tutorial very easily to understand
    Thank You

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

    Thanks for uploading it.. You are doing a great great job.. Please please upload more of such programming questions.. We really need it

  • @rohitdixit1617
    @rohitdixit1617 5 лет назад +3

    Hi Vivekanand. Thanks for the amazing tutorials. Can you please put out a tutorial for matrix reflection?

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

    I tried to implement this algorithm --> first loop works perfect. Seccond loop throws ArrayIndexOutOfBoundsException for i = -1 (which isn't possible)

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

    Thank you very much buddy !!!!!!!! you explained it really well and in a very non confusing manner.

  • @learn-o-land6759
    @learn-o-land6759 Год назад

    Thank you. Great explanation

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

    Thank you that helped a lot. Just had to add in a small fix on the second portion of the code.

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

    I approached this as bfs of binary tree wherein on current element[0,0], insert bottom[1,0] and right element[0,1] in queue. would that approach work ?

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

    AMAZING VIDEO!!! JUST what I needed my friend, thank you!

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

    Is good if you cover rest of the questions that are generally asked in interview... Good Job

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

    Hey peeps, this is kinda different but I wrote this piece of code (JAVASCRIPT) that finds the sum of each of the two diagonals in a square matrix, and it worked for me.
    //square is the name of the 2D array
    int sum = 0;
    //sum of first diagonal
    int sum2 = 0;
    //sum of second diagonal
    int x = 0;
    int y = square.length - 1;
    while (x < square.length)
    {
    sum += square[x][x];
    x++;
    }
    x = 0;
    while (y >= 0 && x < square.length)
    {
    sum2 += square[x][y];
    y--;
    x++;
    }
    //sout sum, sout sum2

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

    Hi Sir, what about [e],[d,j]. means if we come from -1 those diagonal elements are missing

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

    how to check if there is a next element or not?
    Like in the matrix above we don't have a element after 22, how do we determine that?

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

    Clear and concise. Thank you!!!

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

    This algorithm breaks for non square matrices, for example in a 3 x 1 matrix.

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

    For this should work for any (n,m), one needs to have additional conditions in the two while loops, as shown below:
    for k=0:m-1

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

      There is a mistake as well, in the second loop it should be "i = m - 1", instead of "i = m" :)

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

    Thank you so much.. This was very helpful
    Clear and concise..!!

  • @AmbikaKumar21
    @AmbikaKumar21 7 лет назад +3

    *_Please make the videon on Longest common Subsecuence with coding_*

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

    How to do inverse -I mean from a vector to get a matrix in reverse order ?

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

    Bhai... thanku so much....
    Loving watching ur videos...

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

    could you please add a video on in-place rotation of matrix (90 degree) ?

  • @chilukabharath4809
    @chilukabharath4809 6 лет назад +1

    good job sir,
    this question was asked in campus interview.

    • @Ak-um1yg
      @Ak-um1yg 3 года назад +1

      which company man

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

    I'm trying to make a tic tac toe game and this helped me so much haha thanks

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

    Best channel in RUclips!!

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

    sir please explain about antidiagonal matrix where 5*3 matrix

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

    Super b teaching.it is very clear

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

    Sir, this is valid for only m < n

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

    why is k=1 in the second for loop and not k=0 like the first loop?

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

    Could you please share the time nd space complexity for each algorithm

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

    love your videos and explanation

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

    Long live Sir..........Thank u so much.

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

    THANK YOU MISTAH, I REALLY LIKE YOUR VIDIO

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

    very well explained ,thank you sir
    love from bangladesh

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

    Thank you so Much , nice explanation.

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

    At the line i=i-1 inside while loop when we calculate the i it results to -1 , but how can a index be a negative value?

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

    Simple and clear, thank you

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

    Good job!

  • @NileshPatil-si7dy
    @NileshPatil-si7dy Год назад

    it doesn't working in 3rows and 4 columns matrix.

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

    Thanks! Very well explained.

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

    Thanks a lot, that really helped.

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

    What If I want to loop from top to bottom??

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

    Sir can you post a videos related to bigdataanalytics and cloudcomputing

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

    can you help in Write a program to find frequency of user given item in the 3X3
    matrix python

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

    Perfect explanation 👌

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

    Sir could you help me I want to find maximum and minimum local of sin wave by using for loop and if statement

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

    great stuff btw

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

    Great explanation

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

    very clear explanation.

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

    Excellent and thank you so much.

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

    Thankyou so much Vivek sir

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

    Thank you. you are awesome..

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

    Superb 👍👍👍

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

    Very good thanks man for lesson

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

    #include
    int main() {
    printf("THANK YOU ! ! !");
    return 0;
    }

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

    beautifully explained , thank you

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

    Magnificent!

  • @RaviKumar-vk6ib
    @RaviKumar-vk6ib 3 года назад

    excellent!!!

  • @Dadwalfamilyadventures
    @Dadwalfamilyadventures 7 лет назад +3

    As your logic will fails if we have (4, 3) matrix.

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

      change 1st while(i>=0 && j=0 && j

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

    Very Well explained.

  • @Michael-tk1wd
    @Michael-tk1wd 2 года назад

    THATS WHY HES THE MVP THE GOOOOAOT

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

    Thank you for your algorithm

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

    Thank you so much sir

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

    Thank you!

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

    best explanation

  • @DheerajSharma-fs6je
    @DheerajSharma-fs6je Месяц назад

    hats off !!!!!!

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

    clearly understood. thanks.

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

    Python code for matrix of size m*n:
    ----------------------------------------------------------
    no_of_dia=m+n-1
    for k in range(no_of_dia):
    for row in range(m):
    for col in range(n):
    if row+col==k:
    print(num_list[row][col],end=' ')
    print()

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

    Firstly what you printed is called an ANTI-Diagonal. Secondly for a matrix, you're better off naming variables as r,c,R,C instead of i,j,m,n. Lastly here's a simpler for-loop for printing the top and bottom halves of an anti-diagonal for any sized matrix:
    // Traverse top half of the anti-diagonal...
    for (int offset = 0; offset < Math.min(R,C); offset++)
    for (int r = offset, c = 0; r >= 0 && c < C; r--, c++)
    System.out.print(matrix[r][c]);
    // Traverse bottom half of the anti-diagonal...
    for (int offset = R > C ? 0 : 1; offset < Math.max(R,C); offset++)
    for (int r = R - 1, c = offset; r >= 0 && c < C; r--, c++)
    System.out.print(matrix[r][c]);

    • @ريمخالد-ح6ز
      @ريمخالد-ح6ز 4 года назад

      Can you help me please 🥺 l tried to solve but i couldn't. I will show you what i did can you tell me my mistakes?

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

      Hey how is it going on you guys it been 4 years 😂

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

    Thank you sir

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

    sir plz upload a vedio to rotate a matrix k times

  • @SachinSPatil-jm4ls
    @SachinSPatil-jm4ls 6 лет назад

    easy to understand tq sir....

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

    Kindly share me program for sum of the all diagonals of a matrix

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

    i was studying programming but even hindi ^_^

  • @PrinceKumar-eb8hd
    @PrinceKumar-eb8hd 6 лет назад

    very nice video

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

    Sir I want class 12 comp practical comp programing

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

    धन्यवाद