Solving 2D Array Questions | Rotate by 90 degree | GeeksForGeeks | Nishant Chahar Ep-20

Solving 2D Array Questions | Rotate by 90 degree | GeeksForGeeks | Nishant Chahar Ep-20


Solving 2D Array Questions | Rotate by 90 degree | GeeksForGeeks | Nishant Chahar Ep-20

In this video, we’ll are going to solve a 2d array question. We have already solved many questions on the array, so do check out the previous videos in the playlist if you haven’t watched them yet.

Problem link: https://practice.geeksforgeeks.org/pr

Previous lecture:   • Solving 2D Arrays Questions | Make ze…  

You can use my code NISHANT to get 15% Off on all GFG courses.

✨ Important Links ✨
- Don’t Click Here: https://bit.ly/3afmPha
- Telegram Link: https://t.me/codeIn10byNishant
- Github Repo Link: https://github.com/chaharnishant11/Co

✨ Hashtags ✨
#NishantChahar #Microsoft #DSA #Recursion #FAANG #NSIT #NSUT #engineering #internship #college

✨tags ✨
Where to learn dp
where to learn graphs
where to learn dsa
how to start programming
how to start coding
where to learn trees
what is memoisation
what is tabulation
what are graphs
where to learn Operating systems
where to learn dbms
where to learn oops
where to learn computer networks
where to learn low level design
where to learn cs fundamentals
Sanchit jain
Gaurav sen
Gate smashers
Placement guide
How to start programming
where to learn cpp
where to learn python
where to learn javascript
where to learn java
Placement guide
Nishant Chahar Placement Guide
languages to learn
resources to learn data structures
Projects development
AR/VR
Blockchain Machine learning
Deep Learning
Software developer engineer ,
Side projects ,
Importance of side projects ,
Machine Learning Engineer,
How to become a machine learning engineer ,
Associate engineer
Data structures Algorithms
College Life, College, Memories
Fun Fests Chill Enjoy
IITD Mood indigo rendezvous IIT
NSUT Moksha DTU Engifest
Bits Bitsgoa
Namaste Javascript
Namaste Javascript Akshay Saini
Apna college
Apna college c++
apna college DSA
Where to learn dp
where to learn graphs
where to learn dsa
how to start programming
how to start coding
where to learn trees
what is memoisation
what is tabulation
what are graphs
where to learn programming
how to start coding
where to learn coding
where to learn DSA
resources to learn programming
how to crack amazon
how to crack placement
blockchain
what is blockchain
blockchain technology
blockchain technology explained
blockchain explained
blockchain technology in hindi
web development roadmap
roadmap to learn web development
android development roadmap
MERN stack roadmap
machine learning roadmap
roadmap to learn machine learning
roadmap for deep learning
roadmap for 2nd years
roadmap for opensource
roadmap for ios development
roadmap for deep learning
roadmap to learn DSA


Content

0 -> Hi guys welcome back to the channel,
1.05 -> I am Nishant Chahar.
2.278 -> In today's video we are going to talk about two questions,
4.336 -> one question has a dependency on another,
7.056 -> so we'll discuss both of them together,
8.651 -> they are not that difficult.
9.703 -> First we'll do transpose of a matrix,
11.914 -> after that we'll do rotate by 90 degrees.
14.169 -> So we'll do these two questions,
16.543 -> now the last series that was started, the matrix one,
19.298 -> we learned in the last one, how to traverse in a 2d matrix,
23.424 -> and you would have understood that thing, how to check bounds,
26.797 -> it's not going outside left, or right, from up or from down,
30.519 -> we won't do something special in this, it's a very easy question,
34.013 -> so let's start the video without any delay.
36.329 -> You must remember,
37.465 -> most of the people here would have had maths in school,
41.777 -> if not, it's not a problem, we'll learn that.
43.643 -> So, what happens in that, we would always have a question in maths,
46.549 -> do the transpose of a matrix,
48.561 -> find the transpose of this matrix.
49.918 -> What transpose basically is,
52.246 -> transpose is just the swapping of this,
54.79 -> swap these two, these two, and these two.
58.628 -> The diagonal is always same.
63.32 -> Now why is that,
64.133 -> that means, whether you write in coding or maths,
68.092 -> there's a matrix, whatever i, j is given,
71.562 -> make that j, i.
73.124 -> That's what it means.
74.328 -> So, why the middle one stays the same is because,
77.179 -> here 0, 0 is coming, 1, 1 and 2, 2.
81.254 -> So if you do 0, 0, it would remain same
84.819 -> that's why the middle row, this diagonal, not row,
88.647 -> the diagonal always stays the same.
90.815 -> There's not much change in it.
91.867 -> So we have to transpose this.
94.172 -> It's very simple, nothing very difficult.
97.798 -> So, we'll start doing it.
101.548 -> If we see i is equals to 0,
103.724 -> this is the row,
105.082 -> we first do this row, then this row and then this one,
108.397 -> we went to i is equals to 0,
109.744 -> we see the column in 0, transpose this,
114.085 -> if you transpose it, it will just stay where it is,
117.121 -> now there's one thing, what exactly do we do,
118.992 -> we have to replace this triangle with this one,
124.378 -> just replace this bottom triangle with the top one,
129.173 -> so we don't have to traverse in both of them,
132.598 -> because if you keep swapping individually,
135.737 -> you will just get the original from that,
137.662 -> this 1, 5, 9 will remain like this,
142.217 -> I swapped 4 and 2 once,
144.883 -> here 2 has come, here is 4,
147.441 -> did 3 and 7, so they came here,
150.349 -> here will be 6 and here, 8.
152.146 -> When we swapped from this, it reached here,
154.3 -> then again we swapped, so this will again be swapped mistakenly,
158.385 -> this will come back.
160.737 -> Let's show this, it's not a problem,
163.49 -> so if you see, we went to the 0th index, swapped it,
166.949 -> then we went to index 1, swapped at 1, we got this,
171.544 -> then we went to 2, at 2 we got this,
175.069 -> went to i is equals to 0, so we'll traverse in whole of j,
178.929 -> going at j is equals to 0, this will come,
180.751 -> going to j is equals to 1, these two will be swapped,
184.319 -> so we will get this.
185.353 -> Then we'll go to j is equals to 2,
186.501 -> then these two will be swapped, we'll get this,
189.196 -> then we'll increase i is equals to 1
191.868 -> then go to j is equals to 0 again,
193.248 -> here this will be swapped again,
196.374 -> and we'll get this after it is swapped.
198.039 -> Then we'll go to j is equals to 1,
200.169 -> it is 1, 1 so it will just stay here.
202.458 -> Then go to 2,
203.638 -> this will come here, we will interchange them,
208.313 -> 1, 2 with 1, 1.
209.869 -> So this will be interchanged,
211.04 -> we'll get here,
212.139 -> interchanging 2, 1, these two will be interchanged,
214.977 -> these two will be interchanged, 6 and 8,
217.767 -> so here will be 8 & 6,
219.342 -> so here's 6, and 8 will come here.
221.761 -> These two will interchange,
223.192 -> similarly, then we'll go to i is equals to 2,
225.461 -> do the same, 0,
226.957 -> swap this with this,
229.135 -> then we'll get the original back.
231.009 -> It's written wrong here,
233.412 -> 7 & 3 will be swapped,
235.463 -> We'll swap it again, so we'll get this,
238.242 -> this will go on, and then they will be swapped,
239.599 -> they will reach where they were originally,
241.116 -> so the transpose will be done two times if we traverse the whole matrix,
246.269 -> so we don't have to traverse the whole matrix,
248.736 -> we just have to go till, i, until j is less than i,
252.533 -> when we go till j is less than, we'll be travelling in this triangle,
255.704 -> you can try with a pen and paper.
257.231 -> When it's 0, 0, it will be 1, at i is equals to 1
261.257 -> we'll be traversing at 0 and 1,
263.111 -> at i is equals to 2, we'll traverse till 0-1.
268.163 -> In less than, and if we do less than equal to,
270.737 -> then we'll traverse in the whole, but we won't need to do that,
273.016 -> I'll show you the code.
274.748 -> So this is it, you don't have to do much,
276.071 -> just go and swap these.
277.44 -> It has a very easy code,
278.761 -> it really very easy.
281.834 -> What we'll just do is,
292.314 -> I'll first show equal to i to you,
296.305 -> and then just swap,
301.388 -> here we missed an i,
305.49 -> the code has run,
307.088 -> it took 0.09,
311.451 -> so if we don't take it to i,
313.631 -> then maybe it will take a little less time
315.186 -> because we are saving a whole calculation,
318.139 -> because we know there's no change at the diagonal.
320.601 -> It has become 0.08 from 9,
323.041 -> so it took a little less time because of that.
324.87 -> You can see, it was very easy, we didn't do much
327.591 -> we just swapped,
329.107 -> and that's what we had to do.
330.506 -> If you don't want to use this swap function,
331.882 -> you can swap using temporary variable.
334.321 -> The first question is done,
335.773 -> next question is this,
336.964 -> which is the main question that we have to do,
338.076 -> rotate array by 90 degrees.
340.791 -> So rotate matrix by 90 degrees.
342.821 -> So we are given this matrix, we have to rotate it like this.
345.017 -> You can see this was the last output that we got,
349.377 -> when we transposed it, we got,
352.22 -> transposing the matrix, we got 1, 4, 7
355.371 -> 2, 5, 8
357.981 -> and 3, 6, 9.
359.799 -> The rotated is 3, 6, 9
363.29 -> 2, 5, 8 1, 4, 7.
364.899 -> What we need is, 3, 6, 9
367.423 -> 2, 5, 8
368.835 -> 1, 4, 7.
371.195 -> This is the expected answer,
373.461 -> and after transposing we've reached here.
377.493 -> Now pause for a little bit and think what to do.
379.586 -> I mean it is very clear, in front of you,
381.498 -> exactly what we have to do.
383.22 -> And that's exactly what we have to do,
385.412 -> so when you see exactly what is happening here,
387.934 -> these rows are interchanged,
390.24 -> the top row has gone to the bottom,
393.904 -> below that has gone to below the bottom,
395.317 -> and doing this we'll swap all of them.
397.588 -> What we have to do is just swap the rows,
399.791 -> for swapping rows, we have to swap n-i-1 in the ith row,
406.213 -> swap this row with this one,
407.944 -> this row with this one,
409.656 -> one thing is the middle row,
411.687 -> if n is 5, then what will happen is,
414.779 -> middle will be 2,
417.988 -> 2 will be in the middle,
419.89 -> when you swap 2 with 5-2-1,
423.012 -> it will be 2
424.415 -> so you are just swapping the middle one with itself.
427.024 -> You will just be swapping this one with itself,
429.33 -> that's the code,
430.665 -> nothing much will be done in this, we just have to swap the whole rows,
434.313 -> so let's quickly do that.
436.677 -> What we'll do is just reuse the transpose code that we had written,
440.094 -> we did transpose here,
442.947 -> next what we have to do is swap.
445.351 -> Swap rows,
447.251 -> we have to swap rows, from 0
450.019 -> from i we will go till n-2,
453.697 -> we'll do i++.
455.288 -> What we have to do,
456.573 -> just swap the whole row.
464.271 -> So we'll just it again.
466.722 -> We'll just swap, in this i, j will be changed,
470.457 -> i was this, and here j was n-i-1,
476.523 -> and j was the same.
478.494 -> We have to keep j same,
479.68 -> the value should go in the same column,
481.08 -> just have to change it's row.
483.475 -> The column will be same, you can see,
485.283 -> the column is the same.
488.006 -> 6 is where it was before,
490.547 -> 3 is also in the same column,
492.127 -> just i's value, the row has been changed.
495.601 -> So we did just that, changed the row.
497.367 -> Now let's quickly compile it,
499.587 -> So we got what we wanted,
502.225 -> let's submit it.
504.709 -> It has been submitted,
507.35 -> it has taken very less time,
509.464 -> and one thing you'll see here,
511.686 -> if we make this equal to, it will take more time,
514.026 -> that's why I didn't do it again,
515.567 -> since the middle row will generally just be swapping by itself.
518.916 -> So, this is good,
521.525 -> we are doing it by n/2,
523.652 -> and the work is being done.
524.921 -> Thank you so much for watching this video,
526.13 -> I hope you understood the concept conveyed in the video,
528.94 -> these are two concepts, to rotate and
530.822 -> if you don't understand something,
532.026 -> what you have to do is, take a matrix,
535.424 -> first transpose that matrix,
537.279 -> then try to rotate it,
538.528 -> play with matrixes a little,
540.273 -> and it will be done, it's not very complex,
542.899 -> it was a very easy question,
544.195 -> so thank you so much for watching this video,
545.582 -> I hope you liked the video,
546.945 -> we'll meet in the next video,
547.791 -> until then, Bye!

Source: https://www.youtube.com/watch?v=0Cx_U-VSBMQ