Hi everyone.
So in this video we are going to solve this question spider Lord metrics.
Okay so what is input.
So input is basically a matrix and what we have to do so we have to.
I was the matrix and Spider and order.
OK.
I was the matrix in spider Lord.
Now let us consider this matrix OK.
Let us go through this example and let us try to find it.
Spider Lord so spider Lord I will be
OK.
So this is the spider order so its output should be 1 2 3 then 6 9 8 7 4 and 5.
OK.
So this is the output.
This is this battle order traversal for this matrix.
OK.
Now let us perform this piloted traversal cell for this metrics.
OK.
So let's start
OK.
So this is the spider that I was in for this matrix though its output should be one then two then three
then food eight.
Well then eleven then 10 then nine then five then six and then seven.
Okay so this is this pilot that I for this matrix.
Okay.
Now let us perform this pilot traversal for this matrix so this will be then this then this then this
then this then this.
Okay.
So it s spider I will be.
So one two three four.
Then I have eight.
Well sixteen after 16 I have fifteen then 14 then 13 13 then nine then five then six then seven then
eleven.
And then 10.
Okay.
So for this matrix for this matrix this is this battle order but I was 10.
Okay.
Now let us consider one more example and let us find out these by law order for this metrics.
So this will be
so this one.
Okay so let's pilot that is basically one two three four five six then twelve then eighteen then seventeen
sixteen fifteen fourteen thirteen seven eight nine ten and eleven.
Okay so what this matrix this is this pilot and I listen.
Okay so I hope you've got the idea.
So I have to but I was the medics in this fashion
then inside
OK so I have to repeat I was the metrics in this fashion.
OK so we are traversing the medics in this fashion.
OK so this is this pilot I was sitting now let us consider.
Now let us discuss how we can solve this caution.
OK.
So basically this is just implementation problem.
So what I will do I will take for variables.
OK so this is simply an implementation problem.
Now to implement this pilot I whistle What do we need.
So we need four variables.
We simply need for two variables.
Okay so start rule Andrew start column and column.
Okay.
So with the help of these four variables we will be able to solve that equation.
Okay so let us consider a metrics.
So okay so let no do not consider this type of metric so consider this metrics.
So I have this metrics
okay.
So what we do with the value of start rule so start probably be here.
Okay.
So start rule will be here working with the value of Andrew.
So Andrew will be here start column so start column will be here and column will be here okay.
So what I will do first of all I would like trade I have to print in this fashion.
Okay.
In this fashion then inside.
So first I have to print this start.
So let us print this a structural softer printing to start what I will do.
I will print this and column after printing this column.
I will print this.
Andrew after printing this.
Andrew what I will do.
I will print this.
Start column I will print this the start column.
Okay.
So after doing this what I will do I will do basically start plus plus I will do Andrew minus minus
start plus plus because start has been printed Andrew minus minus because Andrew has been printed.
Okay start column plus plus.
Because start column has been printed.
Okay the column we will do and column minus minus because the end column has been printed.
Okay.
I mean a building myself I will do start troop plus plus because this start rule has been printed I
will do Andrew minus minus because the Andrew has been printed I will loose start column plus plus because
the start column has been printed I will do and the column minus minus because the end column has been
printed.
Okay so after doing this what little movie I situation.
So this will be my new matrix
okay.
So this is basically this is basically these start to because I have done started hopelessness this
will be my Andrew because I have done Andrew minus minus this will be my start column because I did
start column plus plus and this will be my end column because I did and column minus minus.
Okay so again we will follow the same approach I will print these start true I will print the end column
I will print the Andrew I will print the start column okay.
After doing this we will perform these four steps again will perform this fourth step again.
So again this will be my inner matrix I performed star listless.
So this will be my star true similarly.
This will be my start.
Gollum because I didn't start Gollum 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.
Okay.
So again we will do the same thing.
Print these start to print the end column.
Print the Andrew and print the start column.
Okay.
And again we will update these values.
So how long I have to perform these operations.
So start rule basically start rule should be less than adequate to Andrew obviously and basically start
column start column should be less than adequate to and column.
Okay so this will be my condition I will write this condition inside of my loop on inside a for loop
and I will perform these steps and I will perform these steps.
Okay so very simple implementation problem.
Okay.
Very very simple implementation problem and let take.
We will also take account variable.
So initially this going to be able to we basically will be zero.
But to have to do so how many elements will be present inside the matrix.
So if the dimensions that I mean.
Okay.
So this is the their mission and crossing.
So how many elements would be present inside the matrix.
So I mean elements would loop present.
So as soon as the count will become it goes to I mean I will stop.
Okay.
So this is one stopping condition.
This is first a stopping condition and this is second stopping condition.
Okay.
So count restored stored how many elements I have printed.
Okay.
So this variable will stored how many elements has been printed.
So that's when is the count of the elements become the question.
I mean that means I have printed all the elements of the matrix then I can stop.
Okay so I have to stop in condition this is the first stopping condition and this one is the second
stopping condition when the count become close to women.
That means I have been and many elements.
So I have printed all the elements of the matrix then I can stop.
Okay.
Very simple.
So just we need 40 variables.
Start rule and start column and column.
Okay.
Initially my star will be zero.
Initially my end column will be zero.
And Andrew will basically be m minus 1 and and column will be n minus 1.
Very simple so this is the equation.
Spiral matrix.
Okay so the input is basically a 2D Matrix and I won't have to print anything I have written that perfectly
just.
Okay.
You can see so if this is the matrix so I have written that perfectly just okay.
Now if you ever understood the logic.
Now let's start writing the code.
Okay.
So as discussed we need for variables.
Okay start.
Okay so let us first find out the size.
Okay.
Let us first find out.
Size.
So M is basically meant to extract size.
How many rules at present.
So many takes a large size and similarly let us find out how many columns are present.
So this will be matrix of 0 large size simple.
Now let us construct our own social construct on set above.
Okay so I have this prof and just set.
Okay so I will push every element inside this answer variable inside this on selector.
Okay now after finding out the size I have to take for variables start rule which will be zero Andrew.
So and there will be m minus 1 and then I have to bake.
Basically start column so start column will be zero start column will be zero and end column.
So what does end columns at this is end minus 1.
Okay so we initialized our variables and I have to also take account variable how many elements has
been printed how many elements has been pushed into this and sort of vector so initially this value
will be zero.
Okay now let us write our condition so vile.
Basically this title is less than our request to Andrew and the start column should be less than order
to hand column.
Okay very simple condition.
Now what I have to do so C carefully first of all I have to print the start.
Okay spider.
Order this one.
So this is the spider order.
So what I have to do I have to first print the title then I have to print the end column.
Okay so this is step one this is step two.
Then I have to print the end through third step and then I have to print the start column Fourth Step.
Okay so in this order.
So this is step one that test by form step 1 So what I have to do so let's print.
Basically these start rule.
Okay I I'm printing this diagram.
So for printing start rule I have to iterate over the columns.
So start column I left tonight it goes straight in column and a plus plus what I have to do.
So long sideboard pushback
I thought pushback metrics.
So this is the metrics.
OK so metrics
so start rule and column is variable again column is a now.
I also have to look on the plus plus.
OK because counties storing how many elements have been printed.
OK so after printing the first after printing the star true what I have to do so I have printed the
star to also start to less plus.
Okay start rule has been printed.
Go to the next title and you can also check.
So basically if the value of count if the value of count to become IT GOES TO M in doing that means
all the elements has been printed.
So basically you can draw down on.
Okay you can read done on set.
So this is the answer.
You're getting it done the onset so step one is done.
Okay so what is it.
Step 2 Step two is very simple.
This one I have to print the end column.
Okay I have to print B and column so that this print the end column
so what bending the end column.
I have quite great all that it was OK to start rule I less than or equal to Andrew I plus press again
to answer dot pushback what I have to lose all metrics off so the rule is variable and I have to print
the end column OK and column and again I have to do count plus plus so let me write the commentary.
So what I'm doing.
So the second step is printing the angle.
Okay I'm printing the N column so after printing the N column let us go back and column minus minus.
Okay and column has been printed and again I will write this condition.
So if the number of elements is it comes to women that means all that element has been printed.
So in that case you can read and the answer allowances Eddie we can draw down I would on set.
Okay.
Now the total step what I have to do.
So this is the third step I have to print the end rule.
Okay.
This is the end.
I have to print.
ANDREW So I have to print Andrew.
So for printing the Andrew I have to print the row in reverse order.
OK.
You can see I have to print that all in this order.
So I will start from and column I will stop at the start column.
OK so I have to start at and column I should be good then I request to start column I minus minus again
answer dot pushback metric self so I am printing the Andrew and the column will vary.
OK.
And again I have to do count plus plus and again after printing the end what I have to do Andrew minus
minus.
OK.
As I have been dead Andrew again you will write the condition.
So if counties it goes to I mean doing that means all the element has been printed so you can write
down the onset.
Okay now a step forward so a step foot is very simple.
So I have to print the start column.
Okay.
I have to print to start column so I will go from.
So this is the start rule.
I will go from Andrew to start.
Okay.
From down to up so now let us print the start column.
Okay.
What I have to do I have to go from down to up.
So I have to go from Andrew I will start from Andrew I will reach these start row I minus minus.
Okay.
Now again answer dot pushback.
So what I have to do so metrics of role will vary so ROIs I and I am printing these start column and
again I have to do count plus plus column distorting how many elements has been printed.
So again after printing the start column I will do start column plus plus.
Okay start column plus plus.
And now you can again check.
So if the value of Countess basically M and when that means all the element has been printed.
So in that case you can pretend you are on set.
Okay.
So I think we have done everything correctly.
Now for the completion purpose since I have written that perfect Tejas.
So let us right denies I here.
Okay so let us write an answer here on TALK
OKAY.
SO WE DID spelling mistake somewhere.
So this will be gone.
Okay now let's go now quote again
okay so our code is looking now in our district to some retired code.
Okay
so I have to check basically the metrics is empty so basically M is zero.
That means metrics is m buddy so in that case you can get done and beyond is that okay.
So at this point.
So this on is empty.
Okay.
If you will run again okay.
So our code is working fine otherwise you can also write one more condition.
So if the number of columns of 0 then also you can draw down the empty onset.
Okay now that some summit
okay.
So our code is working fine.
Now let us discuss the time and the space complexity.
Okay so an order does discuss the timing the space complexity.
So again this base complexity is basically ordered offline.
Okay.
Space complexity is ordered off when we are not looking adding We are not creating any extents.
We are not taking any extra space.
So space is basically order of often and time complexity is basically ordered off and then okay we are
going through each and every element of the metrics we are going through each and every element of the
metric.
So that's why the time complexities I mean m is the number of those and is the number of columns.
Okay.
So it is very simple logic Okay very simple implementation problem.
I am getting the start.
So this is the first to step into the start role then start role plus less so.
Print the end column.
So I'm printing the N column a step two then I am doing and column minus minus then I'm printing the
end.
So I am printing the Andrew.
This is step 3 then I did and minus minus then I am printing the start column.
So this is step put into the start column and then here I have written start column plus plus.
So what will happen this will be my new metrics again I will do.
Step 1 I will lose Step 2 I will lose Step 3 I will lose step forward.
Okay.
So very very simple logic.
Okay.
So this is step one this is step 2 this is step 3 and this is step forward okay I'm in the space complexities
but golf I mean and big offline.
Okay.
So if you have any doubt in this question you can ask me.
Okay.
Thank you.