Hi, everyone.
So in this video, we are going to solve this question, spiral order matrix.
OK, so what is input?
So input is basically a matrix and what we have to do.
So we have to traverse the matrix and spiral order.
OK, we will traverse the Matrix in spiral order.
Now, let us consider this matrix.
OK, let us consider this example and let us try to find its spiral order.
So Spilotro will be.
OK, so this is a spiral also its output should be one, two, three, then six, nine, eight, seven,
four and five.
OK, so this is the output.
This is this pilot, the traversal for this matrix.
OK, now let us perform this pilot or traversal for this matrix.
OK, so let's start.
OK, so this is the spider traversal for this mattocks, though, its output should be one, then two,
then three, then four, eight.
Well then 11.
Then ten, then nine.
Then five, then six and then seven.
OK, so this is this pilot traversal for this matrix.
OK, now let us perform this pilot or traversal for this matrix.
So this will be.
Then this, then this, then this, then this, then this, OK, so it's Spilotro will be so one, two,
three, four, then I have it well 16 after 16 I have 15.
Then 14, then 13.
Thirteen, then nine, then five, then six, then seven, then 11 and then ten.
OK, so for this matrix, for this matrix, this is this paladar traversal.
OK, now let us consider one more example and let us find out the special order for this matrix.
So this will be.
So this one, OK, so it's that is basically one, two, three, four, five, six, then 12, then
18, then 17, 16, 15, 14, 13, seven, eight, nine, 10 and 11.
OK, so what this mattocks.
This is the spoiler.
OK, so I hope you got the idea.
So I have to traverse the murders in this fashion.
Then inside.
OK, so I have to traverse the Matrix in this fashion.
OK, so we are traversing the Murdochs in this fashion.
OK, so this is this pilot, Trevor said.
Now, let us consider now let us discuss how we can solve this question.
OK, so basically this is just an implementation problem.
So what I will do, I will take four variables.
OK, so this is simply implementation problem now to implement this pilot traversal.
What do we need?
So we need four variables.
We simply need for two variables.
OK, so start Andrew start column and column.
OK, so with the help of these four variables, we will be able to solve our question.
OK, so let us consider a matrix.
So OK, so let's not do not consider this type of matrix or construct this matrix.
So I have this matrix.
OK, so what will be the value of Star Trek, so Star Trek will be here.
OK, so Star Trek will be here.
What would be the value of Andrew?
So Andrew will be here.
Start column, so start Collum will be here.
And Colin will be here.
OK, so what I will do, first of all, I would like to read, I have to print in this fashion, OK,
in this fashion, then inside.
So first I have to print this story through.
So let us print.
This is true.
So after printing the starter, what I will do, I will print this and column after printing this and
column, I will print this.
Andrew after printing this.
Andrew, what I will do, I will print this Astarte column.
I will print this start column.
OK, so after doing this, what I will do, I will do basically start placeless.
I will do Andrew minus minus start because a start has been printed and minus minus because Andrew has
been printed.
OK, start column placeless because start column has been printed.
OK and column we will do and column minus minus because the end column has been printed.
OK, I'm repeating myself, I will start RWB plus plus because this starter has been printed I will
do Andrew minus minus because the Andrew has been printed.
I will start column plus plus because the start column has been printed.
I will do and column minus minus because the end column has been printed.
OK, so after doing this what will be a situation.
So this will be my new matrix.
OK, so this is basically this is basically the Stato.
Because I have done start placeless, this will be my end because I have done and minus minus this will
be my start column because I did start column placeless and this will be my end column because I did.
And column minus minus, OK.
So again, we will follow the same approach.
I will print the stato.
I will print the end column.
I will print.
The Andrew, I will print this column, OK, after doing this, we will perform these four steps again.
We will perform this fourth step again.
So again, this will be my Animatrix.
I performed star toplessness, so this will be my star, Drew.
Similarly, this will be my start column because I did start column plus plus this will be my end column
because I did and column minus minus and this will be my Andrew because I did.
Andrew minus minus.
OK, so again, we will do the same thing.
Print this Dutrow print the and column, print the Andrew and print the start column.
OK, and again we will update these values.
So how long I have to perform these operations to start to basically stato should be less than a request
to Andrew obviously and basically start column, start column should be less than articles and column.
OK, so this will be my condition.
I will write this condition inside available inside a for loop and I will perform these steps and I
will perform these steps.
OK, so very simple implementation problem.
OK, very, very simple implementation problem.
And let us take we will also take a count variable, so initially discount the variable will basically
will be zero, but I have to do so.
How many elements will be present inside the Matrix?
So if the damage is not imminent.
OK, so this is the redemption.
And McCrossin.
So how many elements will be present inside the Matrix?
So many elements will be present.
So as soon as the count will become a to women, I will stop.
OK, so this is one strobing condition.
This is first unstrapping condition and the second stopping condition.
OK, so Count will store how many elements I have printed.
OK, so this variable will store how many elements has been printed.
So as soon as the count of the elements becomes close to that means I have printed all the elements
of the matrix, then I can stop.
OK, so I have to stop in condition.
This is the first unstopping condition and this one is the second stopping condition.
When the count becomes close to I mean, that means I have printed an element, so I have printed all
the elements of the Matrix.
Then I can stop.
OK, very simple.
So just we need 40 variables start through and start column and column.
OK, initially my starter will be zero.
Initially my end column will be zero and Andrew will basically be M minus one and end column will be
in the minus one.
Very simple.
So this is the question, spiral matrix, OK, so the input is basically add to the Matrix and I do
not have to print anything.
I have to return that property just OK.
You can see.
So if this is the matrix, so I have to return that property just OK.
Now, if you have understood the logic now, let's start writing the code.
OK, so as discussed, we need four variables.
OK, start.
OK, so let us first find out the size.
OK, let us first find out size.
So M is basically matrix large size.
How many rules are present.
So it takes the right size.
And similarly, let's find out how many columns had burned, so this will be metrics of zero size.
Simple.
Now, let us construct our answer, so I said above.
OK, so I have this that just.
I said, OK, so I will push every element inside this answer variable, inside this answer vector.
OK, now after finding out the size, I have to take four variables stato which will be zero.
Andrew, so Andrew will be minus one.
And then I have to take basically start column, so start column will be zero.
Start will be zero.
And and so what is and this is and minus one.
OK, so we initialized our variables and I have to also take a count variable.
How many elements has been printed, how many elements has been pushed into this and vector?
So initially this value will be zero.
OK.
Now, let us write our condition so vile, basically, the strato is less than equal to Andrew.
And the START column should be less than articles and column.
OK, very simple condition.
Now, what I have to do so carefully, first of all, I have to print the start, OK, spiral this one.
So this is the spiral order.
So what I have to do, I have to first print the STATO, then I have to print the end column.
OK, so this is step one, step two.
Then I have to print the Andrew third step and then I have to print the start column for step.
OK, so in this order.
So this is step one that does perform step one.
So.
What I have to do, so let's print basically these Estado, OK, I am printing this right.
So for printing this story, I have to read over the columns.
So start column.
I left an article, column and I placeless.
What I have to do is push back.
I thought I'd push back metrics, so this is the metrics, OK, so metrics.
So start row and column is variable again, column is a.
Now, I also have to look closely, OK, because distorting how many elements have been printed.
OK, so after printing the first after printing this through what I have to do, so I have printed the
start.
Also start placeless.
OK, start has been printed, go to the next room and you can also check.
So basically if the value of count, if the value of count become it goes to and in doing.
That means all the elements has been printed, so basically you can written and said, OK, you can
read Nonanswer.
So this is the answer.
You understand the answer.
So step one is done.
OK, so what is it?
Step two, step two is very simple.
This one, I have to print the N column.
OK, I have to print the end column so that print the end column.
So for printing the N column, I have to trade or whether it was OK to start.
I listened very close to Andrew I placeless again to answer dort push back.
But after Russo, metrics of.
So raw is variable, and I have to print the end column, OK, and column.
And again, I have to do count placeless.
So let me read the comments here.
So what I am doing, so second step is printing the end column.
OK, I'm printing the end column.
So after printing the end column, let us go back and column minus minus, OK, and column has been
printed.
And again, I will write this condition.
So if the number of elements.
Is it just women that means all the element has been printed, so in that case, you can read and the
answer I want to say is that we can return our answer.
OK, now the third step, what I have to do.
So this is the third step I have to print the Andrew.
OK, this is the Andrew.
I have to print Andrew.
So I have to print Andrew, so for printing the Andrew, I have to print the road in reverse order.
OK, you can see I have to print the role in this order.
So I will start from and I will stop at start column.
OK, so I have to start at the end column.
I should be bigger than I was to start column A minus minus.
Again, on Sadaat pushback.
Metrics.
So I am printing the Andrew.
And the column will vary, OK, and again, I have to count placeless.
And again, after printing the Andrew, what I have to do, Andrew, minus minus, OK, as I have printed
the Andrew again, we will write the condition.
So if this equals to am in Darwin, that means all the element has been printed.
So you can return the answer.
OK.
Now, our step for step forward is very simple, so I have to print the START column.
OK, I have to print the START column, so I will go from so this is the starter.
I will go from Andrew to start.
OK, from down to up.
So now let us sprint.
The START column.
OK, what I have to do, I have to go from down to up, so I have to go from Andrew, I would start
from Andrew.
I will reach these Dutrow.
I mean, this minus.
OK, now, again, push back.
So what I have to do, so matrix of Rowville very slow is I and I, I'm printing the start column.
And again, I have to count placeless count distorting how many elements has been printed.
So, again, after printing the START column, I will do start column placeless, OK, start column
placeless, and now you can again check.
So if the value of count is basically M&A, then that means all the help has been printed.
So in that case, you can return to your answer.
OK.
So I think we have done everything correctly now for the completion purpose since I have to return the
property just so let us right down out here.
OK, so let us try to answer here also.
OK.
OK, so we did spelling mistakes and we're.
So this will be gone.
OK, now let's now Kuragin.
OK, so our court is looking now us straight to some Hidalgo's OK.
So I have to check basically the Texas MBT.
So basically, M is zero, that means Maddux's, and so in that case, you can return Ambrosiano said,
OK.
So at this point, so this answer is empty, OK, if he will run again.
OK, so our goal is working fine, otherwise you can also write one more condition.
So if the number of columns are zero, then also you can return the empty answer.
OK.
Now, that summit.
OK, so our court is working fine now let us discuss the time and the space complexity.
OK, so now let's discuss the time and the space complexity.
So, again, the space complexity is basically order, one space complexity is outdraw when we are not
allocating, we are not creating any stars, we are not taking any extra space.
So space is basically ultrafine and time complexity is basically order of emon.
OK, we are going through each and every element of the metrics we are going through each and every
element of the.
So that's why the time, complexities and the number of rules and the number of columns.
OK, so it is very simple logic, OK, a very simple implementation problem.
I am predicting the start.
So this is the first step into this rule, then starts to print the end column.
So I'm printing the end column.
Step two then I'm doing and column minus minus.
Then I'm printing the end.
So I am printing the intruders step three then I did and minus minus then I am printing the start column.
So this is step print at the start column and then here I have written start column plus plus.
So what will happen.
This will be my new matrix.
Again, I will step one, I will do step two, I will do step three, I will do step forward.
OK, so very, very simple logic.
OK, so this is step when the first step to this is step three and this is step four.
OK, time and the space complexities, big government and big often.
OK, so if you have any doubt in this question, you can ask me, OK, thank you.