Print a given matrix in spiral form | GeeksforGeeks

  Рет қаралды 188,425

GeeksforGeeks

GeeksforGeeks

7 жыл бұрын

Explanation for the article: www.geeksforgeeks.org/print-a-...
This video is contributed by Harshit Jain.
Read More: www.geeksforgeeks.org/print-a...

Пікірлер: 66
@shubham_skadam
@shubham_skadam 3 жыл бұрын
That's really cool solution, Reducing the array makes the logic more memorable
@gouravm4986
@gouravm4986 3 жыл бұрын
Yup
@RahulGupta-ly2uu
@RahulGupta-ly2uu 6 жыл бұрын
i was kicked out from the pi round of my interview for not able to print elements of the matrix in a spiral manner
@Monkeydluffy-we2fk
@Monkeydluffy-we2fk 3 жыл бұрын
Which company! And were r u working now
@spk9434
@spk9434 5 жыл бұрын
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 4 жыл бұрын
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 3 жыл бұрын
Some interviews expect you to do this in 30min
@TheSolidsnake2001
@TheSolidsnake2001 3 жыл бұрын
because these interviewers learned that other big tech companies asked question like this and they are just "monkey see monkey do."
@NikitaKoselev
@NikitaKoselev 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 3 жыл бұрын
@@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 3 жыл бұрын
stop engineering if you can’t solve
@hermesmercuriustrismegistu4841
@hermesmercuriustrismegistu4841 4 жыл бұрын
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
@senthilkumaran5317
@senthilkumaran5317 5 жыл бұрын
Thanks atlast i understood 👍👍 For those who dont get it try it in your computer along with a paper n pen
@naveenbv1838
@naveenbv1838 6 жыл бұрын
Thanks a lot very simple explanation in short :)
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
Thanks for the appreciation naveen :)
@vanshikakumar5739
@vanshikakumar5739 6 жыл бұрын
can u explain how to print array in matrix form
@gyanasahu88
@gyanasahu88 6 жыл бұрын
Need better explanation, quite fuzzy!
@sonalighoshal3212
@sonalighoshal3212 5 жыл бұрын
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.... ???
@VishalYadav-sn3nn
@VishalYadav-sn3nn 2 жыл бұрын
It is only valid for square matrix. Otherwise it would either repeat few elements or does not print few @geekforgeeks
@kishankavathiya8172
@kishankavathiya8172 5 жыл бұрын
Nice solution BTW!!!
@kalaiselvichandrakasu2780
@kalaiselvichandrakasu2780 6 жыл бұрын
plZ tell me the code to find the sum of sub matrices within the original matrix
@pillulove
@pillulove 4 жыл бұрын
m and n are the no of rows and columns, why do you keep saying it as ending row and column index??
@dhruva1221
@dhruva1221 4 жыл бұрын
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
@RaviYadav-bt2eg
@RaviYadav-bt2eg 6 жыл бұрын
For 3rd and 4th case, why do we need to write those if conditions?
@adityapaithon6499
@adityapaithon6499 6 жыл бұрын
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
@dantewhite1818
@dantewhite1818 5 жыл бұрын
could you do a video in java please
@wecan2729
@wecan2729 3 жыл бұрын
class Solution { public: vector spirallyTraverse(vector matrix, int r, int c) { int row = 0; int col = 0; vector output; while(row
@rahularora3792
@rahularora3792 2 жыл бұрын
😆
@plamytommo7809
@plamytommo7809 5 жыл бұрын
the algorithm is working only when [m=n] but it's still a good one tho
@Bhatonia_Jaat
@Bhatonia_Jaat 3 жыл бұрын
how? can you explain pls
@VishalYadav-sn3nn
@VishalYadav-sn3nn 2 жыл бұрын
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 2 жыл бұрын
@@Bhatonia_Jaat Suppose you take a rectangular matrix. e.g. 3x5, the condition for k
@sureshgarine
@sureshgarine 6 жыл бұрын
Thank you.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 жыл бұрын
You're welcome suresh babu garine.
@avtardeepsingh6715
@avtardeepsingh6715 2 жыл бұрын
Thanks Sir
@sabyasachisamadder3612
@sabyasachisamadder3612 5 жыл бұрын
concept u explain is so easy to understand but when you explaining the code your blabbering that's makes it difficult sir
@cutepuppy-casper7561
@cutepuppy-casper7561 2 жыл бұрын
Where is the value of m and n declared?
@tanujmishra2313
@tanujmishra2313 3 жыл бұрын
Thanks
@Centrix1000
@Centrix1000 4 жыл бұрын
Can you please explain the logic to print a matrix in spiral form starting from center (printing outwards)?
@charansaijuturu1076
@charansaijuturu1076 3 жыл бұрын
Simple bro Store all the values and print them in reverse order🤭
@Centrix1000
@Centrix1000 3 жыл бұрын
Charan Sai J sai I fixed it and thats not how it goes 🤫
@divyanshbahuguna6324
@divyanshbahuguna6324 6 жыл бұрын
Very well explained
@sujitamin949
@sujitamin949 6 жыл бұрын
please solve using example while explaining code
@prabalshrivastava6633
@prabalshrivastava6633 3 жыл бұрын
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; } } }
@knseeker3193
@knseeker3193 5 жыл бұрын
Except the last 2 if condition, everything was explained properly
@manojsinghbhandari4280
@manojsinghbhandari4280 3 жыл бұрын
I should start from 0 instead of 1
@BABEENGINEER
@BABEENGINEER 4 жыл бұрын
could have had better variable names....
@sahilanower9189
@sahilanower9189 5 жыл бұрын
Bro thora frame karte waqt samjhate accha hota... Aise hi pehle se code likh liya aur bak diya
@varungole3471
@varungole3471 3 жыл бұрын
Good solution but could have explained better
@shwetankasingh1819
@shwetankasingh1819 5 жыл бұрын
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 3 жыл бұрын
Your teacher must've had some wild expectations seeing as this is a problem that normally appears in interview rounds of FAANG software corporations.
@monuteotia3618
@monuteotia3618 3 жыл бұрын
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 ...
@aakashchaudhary5171
@aakashchaudhary5171 4 жыл бұрын
Thanks!!!
@zRobi97
@zRobi97 4 жыл бұрын
PA
@Kuldeep7720
@Kuldeep7720 3 жыл бұрын
Sir please make videos in hindi language + English notes. That would be better to understand.
Search an element in a sorted and rotated array | GeeksforGeeks
15:16
Trick for spiral matrix traversal
10:12
Techdose
Рет қаралды 195 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,4 МЛН
She ruined my dominos! 😭 Cool train tool helps me #gadget
00:40
Go Gizmo!
Рет қаралды 64 МЛН
Heartwarming: Stranger Saves Puppy from Hot Car #shorts
00:22
Fabiosa Best Lifehacks
Рет қаралды 11 МЛН
Каха ограбил банк
01:00
К-Media
Рет қаралды 11 МЛН
Print Matrix in spiral form ( 2-D array)
17:25
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 123 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 761 М.
Largest Sum Contiguous Subarray | GeeksforGeeks
14:06
GeeksforGeeks
Рет қаралды 219 М.
Convert array into Zig-Zag fashion | GeeksforGeeks
8:53
GeeksforGeeks
Рет қаралды 43 М.
Print Matrix Diagonally (Diagonal order)
13:22
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 109 М.
What is an API and how do you design it? 🗒️✅
15:26
Gaurav Sen
Рет қаралды 714 М.
Sort an array in wave form | GeeksforGeeks
4:33
GeeksforGeeks
Рет қаралды 35 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,4 МЛН