Print a given matrix in spiral form | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 29. 10. 2016
  • Explanation for the article: www.geeksforgeeks.org/print-a-...
    This video is contributed by Harshit Jain.
    Read More: www.geeksforgeeks.org/print-a...

Komentáře • 67

  • @shubham_skadam
    @shubham_skadam Před 4 lety +24

    That's really cool solution, Reducing the array makes the logic more memorable

  • @RahulGupta-ly2uu
    @RahulGupta-ly2uu Před 6 lety +40

    i was kicked out from the pi round of my interview for not able to print elements of the matrix in a spiral manner

  • @spk9434
    @spk9434 Před 5 lety +3

    Good Job ! Reducing the array at the end of every step is the key take away. Thanks for bringing it out. makes it easy to remember.

  • @neoblackcyptron
    @neoblackcyptron Před 4 lety +28

    Why do they ask these questions in interviews? These are super high difficulty. What are they trying to find out in the interviews by asking these questions. There is no way a person can solve these questions in an interview without prior preparation. It will take 1 week for a person who has never seen this question to come up with the solution.

    • @leknubb
      @leknubb Před 3 lety +8

      Some interviews expect you to do this in 30min

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

      because these interviewers learned that other big tech companies asked question like this and they are just "monkey see monkey do."

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

      Multiple reasons I would say. For example, it shows if the candidate has been preparing for that kind of questions. Personally, I would prefer pair programming with a person, or asking theoretical questions on how to solve the task. It is good that a person knows how to make a circular matrix, but pair programming will show me if he/she is up for work.

    • @omkarshendge5438
      @omkarshendge5438 Před 3 lety +5

      @@TheSolidsnake2001 this is so true competitive programming culture is easily catching up to the minds of these interviewers! Google and facebook started asking these type of questions and now even these fintech companies are not behind started following them!

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

      stop engineering if you can’t solve

  • @hermesmercuriustrismegistu4841

    You need to watch this video several times to understand it well. Try folks to trace it. This is a question asked in a Google coding interview! Thx Geeks4Geeks

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

    can u explain how to print array in matrix form

  • @senthilkumaran5317
    @senthilkumaran5317 Před 5 lety +4

    Thanks atlast i understood 👍👍
    For those who dont get it try it in your computer along with a paper n pen

  • @sonalighoshal3212
    @sonalighoshal3212 Před 5 lety +4

    Plz help any1
    What is d role of 2 if conditions...??
    I had ran d code without if conditions
    It worked successfully
    So what is the significance of that 2 if Conditions.... ???

  • @kalaiselvichandrakasu2780

    plZ tell me the code to find the sum of sub matrices within the original matrix

  • @VishalYadav-sn3nn
    @VishalYadav-sn3nn Před 2 lety +1

    It is only valid for square matrix. Otherwise it would either repeat few elements or does not print few @geekforgeeks

  • @RaviYadav-bt2eg
    @RaviYadav-bt2eg Před 6 lety +3

    For 3rd and 4th case, why do we need to write those if conditions?

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

      it will tell how many elements in a row/column are remaining.
      For e.g if you have a 2*6 matrix you don't have any column to print in reverse

  • @pillulove
    @pillulove Před 5 lety +1

    m and n are the no of rows and columns, why do you keep saying it as ending row and column index??

  • @naveenbv1838
    @naveenbv1838 Před 6 lety +8

    Thanks a lot very simple explanation in short :)

  • @gyanasahu88
    @gyanasahu88 Před 6 lety +13

    Need better explanation, quite fuzzy!

  • @dantewhite1818
    @dantewhite1818 Před 5 lety +1

    could you do a video in java please

  • @kishankavathiya8172
    @kishankavathiya8172 Před 5 lety

    Nice solution BTW!!!

  • @Centrix1000
    @Centrix1000 Před 4 lety

    Can you please explain the logic to print a matrix in spiral form starting from center (printing outwards)?

    • @charansaijuturu1076
      @charansaijuturu1076 Před 4 lety +2

      Simple bro
      Store all the values and print them in reverse order🤭

    • @Centrix1000
      @Centrix1000 Před 4 lety

      Charan Sai J sai I fixed it and thats not how it goes 🤫

  • @plamytommo7809
    @plamytommo7809 Před 5 lety +6

    the algorithm is working only when [m=n] but it's still a good one tho

    • @Bhatonia_Jaat
      @Bhatonia_Jaat Před 3 lety

      how? can you explain pls

    • @VishalYadav-sn3nn
      @VishalYadav-sn3nn Před 2 lety

      yes i agree, it is only valid for square matrix. Otherwise it would either repeat few elements or does not print few @geekforgeeks

    • @VishalYadav-sn3nn
      @VishalYadav-sn3nn Před 2 lety

      @@Bhatonia_Jaat Suppose you take a rectangular matrix. e.g. 3x5, the condition for k

  • @dhruva1221
    @dhruva1221 Před 5 lety +6

    I wish tutorial had used code with self descriptive names like k = topRow, l = leftColumn, n = rightColumn & m = bottomRow because I had to come back 3-5 times when the slide got changed into 1st part of the code
    But I found the description straight forward over the same explained in code school channel

  • @cutepuppy-casper7561
    @cutepuppy-casper7561 Před 2 lety

    Where is the value of m and n declared?

  • @sabyasachisamadder3612
    @sabyasachisamadder3612 Před 5 lety +2

    concept u explain is so easy to understand but when you explaining the code your blabbering that's makes it difficult sir

  • @sureshgarine
    @sureshgarine Před 7 lety

    Thank you.

  • @avtardeepsingh6715
    @avtardeepsingh6715 Před 2 lety

    Thanks Sir

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

    please solve using example while explaining code

  • @tanujmishra2313
    @tanujmishra2313 Před 3 lety

    Thanks

  • @BABEENGINEER
    @BABEENGINEER Před 4 lety

    could have had better variable names....

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

    class Solution
    {
    public:
    vector spirallyTraverse(vector matrix, int r, int c)
    {
    int row = 0;
    int col = 0;
    vector output;
    while(row

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

    I should start from 0 instead of 1

  • @divyanshbahuguna6324
    @divyanshbahuguna6324 Před 6 lety

    Very well explained

  • @sahilanower9189
    @sahilanower9189 Před 5 lety +3

    Bro thora frame karte waqt samjhate accha hota... Aise hi pehle se code likh liya aur bak diya

  • @knseeker3193
    @knseeker3193 Před 5 lety +3

    Except the last 2 if condition, everything was explained properly

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

    import java.util.ArrayList;
    import java.util.List;
    import static java.lang.System.*;
    public class PrintSpiralMatrix {
    public static void main(String[] args) {
    int[][] a = {
    {1, 2, 3, 4, 5, 6},
    {7, 8, 9, 10, 11, 12},
    {13, 14, 15, 16, 17, 18}
    };
    int rows = a.length;
    int columns = a[0].length;
    //output expected -> 1,2,3,4,5,6,12,18,17,16,15,14,13,7,8,9,10,11
    //travel
    //left to right -> i=0,0 to 0,columns-1
    //top to bottom -> i=0,columns-1 to rows-1,columns-1
    //right to left -> i=rows-1,columns-1 to rows-1,0
    //bottom to top -> i=rows-1,0 to 1,0
    int i = 0;
    int j = 0;
    List visited = new ArrayList();
    out.println("
    rows : " + rows + "
    columns : " + columns + ",");
    while (rows > 0 || columns > 0) {
    for (; j < columns; j++) {
    if (visited.contains(i + "" + j))
    break;
    visited.add(i + "" + j);
    out.print(a[i][j] + "|");
    }
    --j;
    ++i;
    for (; i < rows; i++) {
    if (visited.contains(i + "" + j))
    break;
    visited.add(i + "" + j);
    out.print(a[i][j] + "|");
    }
    --i;
    --j;
    for (; j >= 0; j--) {
    if (visited.contains(i + "" + j))
    break;
    visited.add(i + "" + j);
    out.print(a[i][j] + "|");
    }
    ++j;
    --i;
    for (; i > 0; i--) {
    if (visited.contains(i + "" + j))
    break;
    visited.add(i + "" + j);
    out.print(a[i][j] + "|");
    }
    ++i;
    ++j;
    rows = rows - 1;
    columns = columns - 1;
    }
    }
    }

  • @varungole3471
    @varungole3471 Před 3 lety

    Good solution but could have explained better

  • @monuteotia3618
    @monuteotia3618 Před 3 lety

    you must elaborate more ..... now i have do a dry run see the function of if statements ......must include that also in explanation.... although its quite good ...i rate 7/10 ...

  • @shwetankasingh1819
    @shwetankasingh1819 Před 5 lety +1

    My teacher had given this program in assignment when I was in class 12th and at that time I tried a lot to solve this problem but didn't get right solution.😑

    • @hasanrizvi2047
      @hasanrizvi2047 Před 3 lety

      Your teacher must've had some wild expectations seeing as this is a problem that normally appears in interview rounds of FAANG software corporations.

  • @aakashchaudhary5171
    @aakashchaudhary5171 Před 4 lety

    Thanks!!!

  • @harshitgupta8922
    @harshitgupta8922 Před 18 dny

    Class 12 ISC student

  • @zRobi97
    @zRobi97 Před 4 lety

    PA

  • @Kuldeep7720
    @Kuldeep7720 Před 3 lety

    Sir please make videos in hindi language + English notes. That would be better to understand.