Transcription
Hey everyone, welcome back to the channel. I hope you guys are doing extremely well. So, this is another lecture of the Scribers A to Z DSA course. In the previous lectures, we have completed step 1.1. So, we'll be starting off with step 1.2, and this is nothing but "Do all these patterns."
You can see that this page contains all the patterns that you have to do before starting off data structures and algorithms. Now, when I talk about patterns, why are they important in order to start off with DSA? Whenever I talk about DSA, whatever topic be, dynamic programming, graphs, trees, arrays, binary search, one thing is common in them that is the loops. If you do not understand loops in depth, if you cannot improvise with loops, then there will be a problem. There will be a problem when you do DSA. That is why patterns are so important because if you want to master something like loops, you want to understand how loops work. If you know how to play around with it, then you can get very, very good at data structures and algorithms. And for loops, you have to do patterns. This is the reason we will be solving a bunch of patterns. Are they asked in interviews? The answer to that is no. I have never seen an interview ask a pattern-based question until I said it's a TCS interview or something like a service-based company. So, patterns are not asked in interviews of the top-tier companies. But patterns do play a significant role when you are starting off with data structures and algorithms.
So, let's look at the first pattern. The first pattern is something like this. So, if I draw that in our iPad, it is something like this. Now, before moving on to the pattern, I'm going to teach you four steps. I'm going to teach you four steps that will help you to print any pattern in the world. Yes. And the first point is, so generically, all the patterns that you will be printing will have nested loops. Again, very important to remember, will have nested loops. And most of the pattern, most of the pattern will have two loops, whereas the outer loop and there is the inner loop. Okay? Now, the outer loop is specifically for the lines. The outer loop is specifically for the lines, and the inner loop is specifically for the columns. If I can say these are the rows and these are the columns, so the four rules are based on it.
So, the first rule states: For the outer loop, yes, for the outer loop, count the number of lines. Found the rows. Found the number of lines. Whatever you can write, count the number of lines, and this will determine what your outer loop is going to be.
The point number two: What did I say? Pattern means nested loops. Nested means outer and inner. So, for the inner loop, for the inner loop, what you have to do is you have to focus on the columns. Focus on the columns and connect them and connect them somehow to the rows. That's the rule number two. You have to focus on the columns and connect them somehow to the rows. I'll understand others afterwards.
Now, whatever you're printing, print them inside the inner loop. Whatever you're printing, print the, let's say, over here it's a star. The other places can be one, two, can be a, b, c, d, whatever you're printing, print them inside the inner for loop. And I'll show you an example. Everything will get clarified.
The fourth one is: Observe symmetry in case of some patterns. Observe symmetry. So, this is an optional one. This step is an optional step. Why? Because this step will be only applicable to certain patterns, not to all the patterns. So, this is an optional step. But the first three steps are mandatory. The fourth step is an optional one. We will solve patterns which will be requiring observing symmetry.
So, let's come to the first pattern. I'll be explaining this in depth, and then the next patterns, we will be moving faster. If you look at this pattern, let's cut the first rule for the outer loop: count the number of lines. So, if you see, it is actually doing some tasks for four times, right? It is exactly doing the task for two times. So, the outer loop will be running for four times. Or you can say it'll be like `i = 0; i < 4; i++`. Can I say this is what the outer loop will be? So, this is going to be your outer loop, pretty simple and straightforward. This is your outer loop. So, I can say I have somehow done the step one, which is printing for the outer loop. Okay? So, we have, we are moving for the outer loops.
Now, the next step comes for the inner loop. Focus on the columns for the inner loop. Focus on the columns and connect them somehow to the rows. Let's see. We have four rows, and what is happening at every line? At every line, what is happening? We are printing four stars. At every line, what is happening? We are printing four stars. At every line, what is happening? We are printing four stars. So, can I see if I try to write it? Can I see for the zeroth row, I am printing four stars? For the first row, I'm printing four stars. For the second row, I'm printing four stars. From the third row, I'm printing fourth star. Like, if I take zero-based indexing, can I say I'm printing four, four, four? Can I connect it somehow to the rows? Yes, the total number of rows were four, and at every line, we are printing those many stars. So, can I say the inner loop will be very straightforward and it will be running for `j = 0; j < 4; j++`? Can I see this? This will be your inner loop. So, can I say whenever `i` is 0, `j` will be 0, 1, 2, 3? Whenever `i` is 1, `j` will be 0, 1, 2, 3? Can I say whenever `i` is 2, `j` will be 0, 1, 2, 3? Whenever `i` is 3, there will be 0, 1, 2, 3? It will be. So, step number two is also completed because we have connected at every line, or whatever, at every column, whatever is happening to the number of rows, somehow I've connected them.
Now, since I've connected them, what's my next step? Print whatever you're printing inside the inner for loop. You have determined what has to be done. Now, what are you printing? Stars. Go ahead and just print those stars. That's it. But there is a certain thing that you have to keep in mind. You print them inside the inner for loop, but make sure since everyone is an individual line, at every, like, once you have done everything for the first line, you have to actually go to the new line. Once you have done everything for the next line, you have to go to the new line. So, what you will do is you'll just give away a print newline over here. If you're using Java, it will be `System.out.println()`. If you're using C++, it'll be `cout`. And what this will do? Now, let's do a typical dry run. Let's see what happens. For the first time, `i` is 0. Whenever `i` is 0, what happens? `j` is 0. So, `j` is 0, goes and prints the star, right? And then comes back. `j` is 1, prints the star, comes back. `j` is 2, prints the star, comes back. `j` is 3, prints the star. Once `j` is 3 and it comes back, `j` becomes 4, and this condition is false, and this for loop is done. Now, what happens? It goes to `cout << endl`. So, that means it goes to the new line. When it goes to the new line, `i` is this time 1. And whenever `i` is 1, it again starts `j` from. It again starts `j` from 0 and again starts printing. `j` becomes 1, again starts printing. `j` becomes 2, again starts printing. `j` becomes 3, again starts printing. And then the end happens, and it goes to the next line. Then `i` becomes 2. Again, the same thing will happen. Then `i` becomes 3. Again, the same thing will happen. And eventually, `i` will become 4, and this particular condition will turn out to be false, and I can say my entire pattern is printed on the screen. Quite easy.
So, this was step number three. Do we have, do we have to observe symmetry? No, in this case, no symmetrical observation has to be done. I'll show you patterns where you have to do it. So, we'll be super quickly coding this up. Just a case for loops in C++ and for loops in Java are the same. So, it doesn't matter which language I'm coding in. You can code it in Java as well. I'll be leaving the notes link in the description, which will also have the Java code. So, if you want the Java code or the Python code, you can just open it up and you can keep it on the side, just to check out how everything works. Okay? So, we have `int main()` in C++. In Java, it will be `public static void main()`. Imagine, imagine I, I just write a function which is going to do that task for me, which is `void printPattern1()`. I'll probably print pattern one. And I see the pattern was something like `for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cout << "*"; } cout << endl; }`. And in order to execute this pattern, I have to call it from here, correct? And what I'll do is I'll go ahead and run this task. Let's see on running what happens. So, when I run this, you will see this is getting printed. But usually, you do not do it like four or five. What they do is they give you an input. So, probably imagine I give you an input, 5. So, what happens is you take `n` as the input, and this input is passed over here. You remember function? So, you give `n` over here, and if you write `n` over here, now what will happen is, based on how many rows you want, this pattern will get printed. So, now again, I'll go and run this task. So, you need five lines, it gets printed. You need seven lines, it will get printed. So, depending on however lines you want, you can just give it as an input, and this function will make sure this particular pattern is printed.
Now, we need to learn about the online compiler. When you go for interviews or whenever you're preparing for placements, you will not be coding into `main`. This is something you will not be coding up. In compilers or in coding rounds, what you just need to do is you just need to write this function. Now, all of these things will be hidden. So, let's go to an online compiler. So, this is Code Studio by Coding Ninjas, which I'll be using for this particular video. Now, this is similar to all the other compilers, and you have to use online compilers when you're preparing for coding interviews. You just can't code in your local compilers. So, get used to this as of now. So, what I'll do is I'll just take this particular piece of code and I'll try to copy this over here and see if it is running fine. I'll try to run it. The moment I run it, I see actually wrong. And this is where you can actually check out where it is running. So, they are saying they have given two, three, five. Now, you are saying, but driver, we just had one value. Now, what they are doing is they're probably doing something like this. I'll explain. Two, it's saying the program runs for two texts. Then they're giving something like three and five, right? Three and five. So, what they are saying is, basically, "Hey, listen, this time we'll take test cases. Our pattern function should run these many times." And what they do is they go ahead and probably divide something like this: `T` and `i++`, and inside this, inside this, they take this particular `n` and they call the function. So, basically, two times you will have to print patterns. First time for three rows, the next time for five rows. So, if you run it in the local compiler, see what happens. Let's run that in the local compiler. So, the moment, sorry, my bad. So, the moment I run that in the local compiler, you will see what happens. First, just got printed. Next, this. So, this is the meaning of test cases. Test cases means your program internally is run for multiple cases. They will never run it for one test case. In the real life or in the coding world, there will be thousands of test cases. Any program that you run will be run on, any program that you write will be run on thousands of test cases. Like over here, your program is run on two test cases, and this is how the backend looks. In the backend, they will write something into `main`, they'll take the test cases, and they'll call your functions for every other test case, and your function has to perform the task for the first time, three rows, or the next time, five rows.
Now, you might be thinking, "Okay, but I was right, why was it wrong?" If you see, they're clearly showing that there has to be a space. Because if you look at this, they want space. What you can just give is there's a space. And once you've given the space, you can go ahead and run it again. The moment you give a space and run it again, you see that it is running fine. You can go ahead and submit this code, and you will see that this is giving you a correct answer. So, from now onwards, you should always practice on online coding platforms because when you go for interviews, this is where you will have to give your online exams or whatever it is. So, please start practicing.
So, I hope you have understood the test cases stuff and how the backend looks in an online compiler. What you have to write, you don't have to write the entire code, you just have to write the function snippet because the other things, they will be taken care of.
Let's look at the second pattern. Let's try to again implement this same number of steps. So, the second pattern states, okay, we have one, two, three, four, five. Five rows. The first loop is going to be, uh, that straightforward. So, let's do one thing. Let's quickly, uh, copy-paste this stuff and, yeah, let's do it. Pattern number two. I'm going to do is, I'll do a `printPattern2()`. I'll probably just, yeah. So, what is the first loop? First loop is saying, again, `n` rows. That is very obvious because we are seeing that it is for one, two, three, four, five rows. That's something which we are definitely seeing. Let's look at the step one is done. Without a loop, let's look at the inner loop and try to connect it somehow. So, if I look at for the zeroth row, I'm just printing one star, right? And I say for the zero, three printing one star. For the first row, I'm printing two stars. For the second row, I am printing three stars. For the third row, I'm printing four stars. For the fourth, I am printing five stars. So, can I say, can I say I have to run it for once for the first line and run it for twice in the second line, to run it for thrice in the second line? Can I see if it is line zero, it runs for, or if it is line one, it runs for once, twice, thrice? Over five times? Very simple, isn't it? So, can I say if you're at the inner loop as `j = 0; j <= i; j++`? And then inside this, if I give something like `cout << "* ";` and over here I `endl`, will this run? This has to run, isn't it? This has to run. So, I've given the test cases, everything. Will it run? Let's give it a try and see if it is running fine. Let's run this task. So, if you see over here, this is, this did not run because why? Because we are calling `print1`. So, we will call `print2`. My bad. It's called `print2`. And let's see if it is, if it runs. It did. It's pretty simple, isn't it? When `i` is 0, it runs from 0 to 0. When `i` is 1, it runs from 0 to 1. When `i` is 2, it runs from 0 to 2. So, we just provide, yes, the step number two. We just looked at the step number two, and it was pretty simple. The step number two clearly stated, "Connect it somehow to the rows," and we were seeing zero row, one row. It was pretty simple, just run it for zero, one time, two times, three times, and that was very evident to be connected to the outer `i` loop. And the third step was, "Print them inside the for loop." So, the three steps were done, and I was easily able to print this particular pattern. Again, let's, for the online compiler, let's take this for the online compiler. Let's take this and try to take it over here and try to run it and see if it is running fine. It is. And let's submit this. So, if I submit this, this is running fine. Yes, it is. So, that was the pattern number two. And let's go to the next pattern. That's the pattern number three. It's something like, uh, one, one, two, one, two, three. Again, step number one for the outer loop: count the number of lines. One, two, three, four, five. So, it has five lines. So, the outer loop is going to be very, very straightforward. So, let's again take this and copy this for the pattern number three. So, this is going to be pattern number three, and the outer loop is going to be pretty straightforward, `0` to `n`. And, yeah, let's take this off. And this is going to be, uh, three. So, the outer loop is tracing. But now, let's try to connect the, the inner loop, like the columns, to somehow the rows. Now, I know this is row number zero. For the order of simplicity, let's take this as row number one. For the row number one, I'm actually printing from just one. For the rule number two, I'm printing one, two. For the rule number three, I'm printing one, two, three. For the row number four, I'm printing one, two, three, four. So, can I say for every row number, I'm actually printing from one to row number? Can I say for every row, I'm printing from one to one? Second row, one to two. The fourth row, one to four. Fifth row, one to five. Can I say for every row, because I know the outer loop runs for every row, so can I say for every row, and we are taking simplicity like one, so we will do `i - 1`? Can I say for every row, the columns are going from one till the row itself? And then `j++`? And what are we printing? We are printing nothing but, let's look at this, what are we printing? One, two, three. That's nothing but the inner loop increment. That's it. Let's go ahead and print `j`. So, if you print `j`, will it work? It ideally should. Let's quickly run this and see if it is running fine. Run task and compile and run. Let's see. So, we are seeing, guy, does it, does it run fine? So, what I'll do is I'll take it to the online compiler, paste it over here, and see if it is giving us the correct answer. It is. Submit this, and it will be giving you the correct answer. So, the third pattern is also done.
Now, let's look at the fourth part. Let's look at the fourth part. What difference do you find? It is quite similar to the previous part, but the thing is, on every row, we are printing the row number. We're not incrementing it like one, two, three. Can I see we're just printing the row numbers? So, if I go back to our stuff and do the pattern print of code, this copy-paste the same thing, just, yeah, let's copy this same thing and close this. Do this pattern four. And instead of this, can I say on every column or on every row, I'm just printing the row number in itself? And I'll try to now run this and see if it is running fine. If you ready to run this, you will see that, yes, yes, no, it is not. Because why did, oh, sorry, `print4`, my bad. Let's do this. Run task and see. Is it running fine? It is. Very simple. You see, one, one, one, and all of them are running absolutely fine. I'll just go over here and paste it and really try to submit it. Num, super confidence. I'll just directly submit it. It is running fine. The pattern number four is done.
Now, let's talk about the pattern number five. So, when I talk about the pattern number five, step one, very simple. For the outer loop, count the number of lines. It's one, two, three, four, five. That's `n` lines. So, without thinking anything, go ahead and copy this and do the pattern number five. Erase this and let's be like, "Okay, this is running for five tips for sure." Without a loop is done. Let's now analyze for the inner loop. For the inner loop, can I say on the row number one, there are five? It's printing five tips. So, let's, let's analyze. One prints five. Ten. The first row prints five stars. The second row kind of prints four stars. The third row kind of prints three stars. The fourth row kind of prints two stars. The fifth row kind of prints one star. So, if I have to somehow, somehow connect them to the rows, can I see? Can I see this? The total rows minus the row number plus one is what you're printing on every row. Again, observation, very simple observation. Can I see this? I can. Why? Because if it is one, and the total are five, `5 - 1 + 1` is five, is what you're printing here. If it is two, `5 - 2 + 1`. This is nothing but four, is what you're printing. So, this is what you are printing. These are the number of stars you're printing. So, can I say the inner loop is going to be very simple? `j = 0; j < n - i; j++`? Again, I'm starting from 0. So, my formula is this. If you start from 1, your formula might be something else. What matters is how many times you're running. Okay? `cout << "* ";` and then go ahead and `cout << endl;`. I will not be submitting this time on the online compiler. All the problem links, you will be finding it in the description. Or it did not work again. Print five. Let's quickly compile this. I'll not be submitting this code on the online compiler anymore. All the problem links you can find it in the description, and you can go ahead and submit them by yourself when you're practicing. So, the pattern number five is also done. You see, one rule solves almost all the problems.
Let's look at this again. Very similar. It's like one, two, three, four, five. One, two, three, four. So, it's like printing from one to that row number. Instead of this, you have to print the numbers. So, it's pretty, pretty easy. I just copy-paste and it goes like pattern six over here. I can do a pattern six. And this time, you can just go ahead from here. This is like this, and you can be like, "Okay, `j`." And this should be fine. Let's quickly run this for pattern six. Again, pretty simple. So, we do this. Yeah, it does run fine, right? So, if you see, by the way, let's, for easy understanding, let's do one test case at only seven. Yeah, that should give you an easy understanding. So, if I run this, you see everything is running absolutely like this. So, this is also done.
Now, this one is where you have to think a bit. So, probably pause the video and think on how you can solve this. So, what is the first rule? The first rule is very simple: count the number of rows. That's five. First loop is definitely going to be something from like `i = 0` until, uh, five, which is like, you can run it till four. So, this is something which I know. So, if I try to write down the row numbers, it's like 0, 1, 2, 3, 4. That's very obvious. What's the second rule? The second rule states: for the inner loop, focus on the columns. What is happening and connect them somehow to the rows? First thing that I'll see is how many columns are there? So, can I say it's like one, two, three, four, five, six, seven, eight, nine? So, can I say in total, in total, there are nine columns, right? And if I carefully observe, what are we printing? We're going to print space, stars, space, space, stars, space, space, and stars, space, space. So, we are printing nothing but space, stars, space. So, if we logically think inside it, if we can print space, and if after that we can print stars, and after that if we can print space, I think our job will be done. Now, let's analyze for every row, how many spaces we have to print, how many stars we have to print, and how many spaces we have to print. If you are able to do this, I think we will be able to write our inner loops very easily, isn't it? So, can I say, the zero, uh, zero column, what are we printing? Printing one space, two space, three space, four space. It's like four space, one star, four space, right? Okay, can I say over here? It's like three comma three comma three. Can I say over here? It's two comma five comma two. Okay, can I say over here? It's one comma seven comma one. Can I say, oh, yeah, it's zero comma nine comma, is it nine? Yeah, it is zero. Can I say we're printing these many spaces, these many stars, and these many spaces? Yes, we can. Now, we need to find what's somehow connected, somehow to the rows. I think we can. We know how to connect. So, if you look at this, this is nothing but going in the reverse direction of the number of rows. The number of rows are from zero to four. So, it's pretty, can I say the formula for the first space is nothing but `n - i`? Which is, just imagine for the first case, `i` will be zero. So, if `n` is 5, those are the five rows. `n - i - 1` is what this will be. Very obvious. So, I have figured out the space to be this. You can just do some math and you'll be able to figure it out, assuming `n` is 5 over here. So, this is very simple: `5 - 0 - 1`. For the first time, next time, `5 - 1 - 1`. Next time, `5 - 2 - 1`. So, so on. So, space has been computed. But now we have to think about the stars. How do we compute the stars? Because the stars are like one, three, five. It's somehow increasing because zero is one, for one it is three, now four it is nine. It's something like one more than twice. Can I see it's something like `2 * i + 1` is what it is? Can I say this? `2 * 0 + 1` is 1. `2 * 3` is 6, plus 1 is 7. So, can I say the stars is `2 * i + 1`? So, I've kind of figured out the relation, somehow connected to the rows. Just run the inner loops. The only difference is this time, the inner loop will first print the space, then the stars, and then this. So, there will be three different inner loops. First, this inner loop, then this inner loop, then this inner loop. This one for space, this one for star, and this one for space. Very simple. I just broke down the problem into simpler ones. Once I've done this, I think my job will be done. So, let's quickly write the pattern number seven. Let's write the `void printPattern7(int n)`. Let's take `n`. So, I know one thing, `i` will be starting from zero. Again, I'm doing it in terms of zero-based indexing. You can go ahead and do with whatever base indexing you feel like. One-based. Let's print the space, then the star, and then the space. Okay? So, this one will be for `j = 0; j < n - i - 1; j++`. And then `j` can go as plus plus. Very obvious. Uh, `k`. Let me just quickly check. `j < n - i - 1` because this should be fine. And the similar thing will be at the last as well. And for the stars, it's very obvious that you go from, sorry, `j`. `j` goes from 0. `j` goes on till `2 * i + 1`. And then `j++`. Quite easy. This will be space. So, you can just go ahead and print it something like this. This will also be space. And this will be stars. So, go ahead and print the stars. And once everything is printed, you, right before moving to the next row, you move on to the next line. So, let's go ahead and print the part number seven. My bad. Let's print the pattern number seven. Let's go to the terminal and run the test and see where it's running fine. So, if I run this, you see that this particular pattern is indeed getting printed. So, the main task is to break down the problem into this, into parts, and then follow the rules that I have taught you. Okay?
This one, pretty similar. The inverted one. Can you do it in the inverted fashion? I think you can. What is the, this time you start from, this will be zero space, this will be one space, this will be two space. So, it goes in the opposite way. The stars are in the opposite fashion as well. So, think on this. Pause away, pause the video and think on this. And let's see if you can do this. So, coming back to the pattern, what's the first law? The first law is very simple: zero, one, two, three, four. So, the first loop is going to be super easy to predict. Now, we know we are printing space, then we are printing stars, and then we are again printing space. This is something we know for sure. Now, the question is, how many spaces? Initially, if we look at this, initially for zero, we're printing zero space, and you're printing like nine stars, and then zero space. One, it is something like one space, seven stars, one space. For two, it is like two space, and then, uh, five, and then two. Or three, it is like three, this is three, and this is three. For four, it is like four, this is one, and this is nothing but four. So, the space is one. It's pretty simple. It's pretty simple. So, if I just go back, and we know, uh, the pattern seven is kind of similar. So, we'll just copy-paste and we'll just take this off and make this pattern something. I know for sure is the space one is equivalent to the row. If there are, is the zero through, there will be zero spaces. So, this won't run. This is something which you know for sure. The only problem is the stars. The formula is going to be super simple. If `n` is 5, for 0, it'll be like `2 * n - 2 * i + 1`. You can try doing it. And if you apply this formula for the zero through, it is going to be like `2 * 5 = 10 - 2 * 0 = 0 + 1`, it's like `10 - 1 = 9`. For this one, it'll be like `2 * 5 = 10 - 2 * 2 = 4 + 1 = 5`. `10 - 5 = 5`. So, it's very simple. What I did was, in the previous problem, I knew this was nothing but `2 * i + 1`. And I saw it as reducing. So, reduce it from twice of `n` is quite simple. So, what I'll do is I'll go over here and I'll say `2 * n - 2 * i + 1`. Again, you can do it as you prefer. There is no hard and fast rule that you have to do it in this way only. You can assign variables, reduce them, and do, uh, however you wish. Like, so I'll go and print this. Print a terminal. Run task. Okay? And run it. Looks like it is not getting printed in this way. Okay, so it has to be `2 * n`. No, I think it's shipping. Run task. Clean and run. Okay, it does print. So, this is how, uh, easily we can do the pattern number eight. It's time to do pattern number nine, where the fourth rule will come. So, pause the video and maybe think how you can do on this. Pattern number nine looks a combination of seven and eight, isn't it? So, what you can always do is, you can go ahead and combine both of them. So, imagine I say, "Let's first print, print num pattern number seven according to this particular `n`, and then print pattern number eight." So, if I do this, will it run? It logically should. And you will see it does. So, sometimes you have to be smart. It's not necessary that you always write patterns. You can always combine them as well. You can combine two different patterns to generate one as well. So, keep that in mind. And that's actually a good way to write patterns, combining. So, the pattern number nine is completed. Time for the pattern number ten. Again, this looks quite similar to one of the ones that we did, isn't it? But is it a flip pattern? Like, this one was a complete flip pattern. Like, the same lines. If you see this line and if you see this line, they were similar. Right? Over here, we have a, we have a right-angled triangle. But the bottom one is like, there are two same lines of equal number of stars. Here, there is no. So, you cannot merge, one, like, straight, triangle, right-angled triangle, and the inverted right-angled triangle. You cannot do that over here. So, you can do it with some fluctuations. But I'll still. So, let's look at this pattern. So, `n` is given as 5. First thing to observe is the rule number four, symmetrical. Now, this pattern is a symmetrical pattern. Observe symmetry. Where does it get? This is the point where it starts getting symmetry. So, whatever logic you have to write will be okay till here, and then you have to flip the logic. So, observe symmetry. You have observed symmetry. That is done. What is the outer loop? Let's see. One, two, three, four, five, six, seven, eight, nine. Nine rows is what you have to run. Nine rows is what you have to run. So, can I say if you're running nine rows, it's very obvious to say you're running `2 * n - 1` times. That is what your outer loop runs. So, going back to the code, the outer loop is pretty similar, pretty straightforward. `i = 0; i < 2 * n - 1; i++`. So, this is what the outer loop will be. So, let's quickly write this. One, two, three, four, five, six, seven, eight, nine. So, we have written the outer loop. Now, let's talk about the inner loop. Now, when I say inner loop, what does that mean? It means this one. For the first time, I'm printing one star. Second, I'm printing two stars. Third, I'm printing three stars. Fourth, I'm printing four stars. Fifth time, I'm printing five stars. Something I'm sure is, till here, till the symmetrical portion, the number of stars that I am printing is definitely equal to the row number. So, let's, let's say the number of stars that you're printing is equal to the row number, which is `i`. So, we can go ahead and say `j = 1; j <= stars; j++`. So, we go ahead and say `cout << "*";`. It's actually correct, right? And if you run this, let's see what will happen. Okay, go ahead and try to run this. This will like print, but this will print something like this, whereas from the row number six, you have to break the symmetry. From the row number six, you have to break the symmetry. Let's try to analyze a formula for row number six. We need four stars. Seven, we need three. So, eight, we need two. Or nine, we need one. I think it's pretty evident. The formula, the formula is crystal clear: `2 * n - i` is a formula, isn't it? Those many stars is what you require. If you exceed the row, if you exceed the fifth row, because this is your symmetrical position, from six downwards, these are the number of stars that you will be printing. So, can I say if the row exceeds the `n`th row, if the row exceeds the `n`th row, then the stars that you will be printing will be nothing but `2 * n -` the row number? Right? Can I say this? Row means `i` over here. Can I say this? I think I can. Go and run. Run the task. Well, let's print this. Yes, it does. Very simple. If I try to write it, uh, in a much simpler way, like you can just write it in other ways as well, ternary operators, whatever you wish to. But this is how you can easily print the nested or loops. So, we are done with the pattern number 10 as well.
Let's look at the pattern number 11. Okay, we have done a similar pattern over here. The right-angled triangles. Uh, I hope, uh, I don't remember if we called that. So, the row numbers are like five. That is quite easy. So, going ahead, `void print11(int n)`. And you can say, I know one thing, `i = 0; i < n; i++`. This is definitely the outer loop. There is no doubt with the outer loop. Let's look at the inner loop. How's the inner loop working? Can I have the zero throw, starts with? For the second row, it starts with one. For the third row, it starts with, sorry, for the fifth. So, it's like. So, can I say for the zero throw, it starts with one? For, uh, the second row, it starts with one. Now, the fourth row, it starts with one. Can I say for all the even rows, but all the even rows, it starts with one? Okay? So, I'll be like, what's the start point? Let's keep `start` as one. And can I say this, if I am an even row, a star is actually one, or else the star is actually zero? Can I see this? Maybe you can define this somewhere here and assign it some dummy value if you wish to. So, can I say if it's an even row, the start is one, as the start is zero? That is something which we know. For every column, now what happens? It prints for one time, two times, three times, by flipping, flipping, flipping. One, zero, one, flip, flip, flip. So, can I do this? I think I can. It's very easy. What I'll do is, I know how to print the right-angled triangle. That's something like this. This, this, this, this, this. And you just print the star, isn't it? You just print the star. And pause this. Your print handle. But if the start is one, it stays as one. The next step, it has to be zero. It flips. So, you can say, "Flip the flip is very easy." You can write `start = 1 - start`. This line will flip one to zero and zero to one. You can do math and you'll find. Now, let's, uh, print pattern 11. Yeah, let's try to print it and see if it is running fine. You see those three or four rules will actually print everything. Uh, did we print it properly? Let's quickly check. I should have started from one. Looks like the first line was not printed. Okay, my bad. It should be `j <= i`. So, run task and clean and this will be printing it. One, zero, one. The pattern is printed. Pretty easy, isn't it?
Let's look at the pattern 12. Uh, you can probably pause the video and try out the pattern yourself. This pattern looks, so this pattern, uh, so this pattern kind of looks similar to the pattern that we did. Uh, if you remember, yeah, when there was space, stars, space. And over here, can I say it is like number, space, numbers? So, if I try to write this pattern, can I say that if I can figure out the way of numbers, space, numbers, I think my job will be done. That's tracing. But for the outer loop, that's very easy. You just figure out how many. Can I say I will be just running the outer loop for four times? The outer loop is very simple. It runs for four times. The outer loop will be running for four times, right? So, if the outer loop is running for four times, let's look at this. For the first row, can I say I'm printing one number, right? And one number. But between that, I'm printing some spaces. Let's count the number of spaces. One, two, three, four, five, six is the number of spaces that I'm printing. Perfect. Let's look at the next one. Repeating two numbers, two numbers, and let's count the number of spaces. One, two, three, four. Okay. Depending four spaces. Let's count the next one. Three, three, and the spaces are one, two. Next, four, space, zero, and then four. Something I'm sure is the number and this one are very easy because it's equivalent to the row number. So, what I'll do is I'll go ahead and first, uh, try to write this pattern, and then we can figure out the other one. So, I'll take the `int n`. I can do is I can just, so print 12. And I know one thing, you can just start from one. Again, you can do zero-based indexing, that is your choice. So, first is numbers, then a space, and there is numbers. So, can I say the numbers are going to be very simple? It can go from one till the `i`. And `j++`. Take it. Go ahead and say `j`. And this, not this. Yeah, perfect. And similarly, you can just do it in the opposite fashion because if you look at this, it's like three to one. It goes in the opposite fashion. So, it's sky easy. You just go from `i` to one and you do `j--`. So, this is what you do for the numbers. But space is what you have to exactly determine. The next thing that I need to do is I need to figure out a formula for space because if I can figure out a formula for space, it can be very easy. So, can I see the formulas? We start from six and then do you see a change? It reduces by two. It always reduces by two for sure, right? So, if you can somehow figure out the first number, and then you can just reduce it by two at every step, I think the job will be done. So, can I say the first number? First number is nothing but `2 *` whatever is `n - 1`. So, first, very soon, because if `n` is 4, it's definitely six. Right? If `n` was three, it would have definitely been two. Sorry, four. So, `2 * (n - 1)`. And that then at every step, you just reduce it by two. Quite simple. So, what I do is I say, "Hey, listen, I know this, the space initially that you have to give is this." Sorry, oh my bad. Going to `n - 1`. This is something which we know. Why don't you go ahead and say, "Okay, please go ahead and print those many spaces." So, it's like space. And then you can do `j++`. And then you're going to `cout << " ";`. Perfect. And right after every step, you definitely are going to do an `endl`. But at the same time, you say `space -= 2`, which is equivalent to `space = space - 2`. And you can go ahead and say `printPattern12(n)`. And okay, you can go to the terminal and say, "Run task. Clean and run." If we click and run, you actually see that this pattern is getting printed for the fifth one as well. Pretty easy, isn't it? So, with this, we will be wrapping up the pattern number 12 as well. The next pattern is, I think, very easy. You can try it on yourself. So, the pattern number 12.
Is done now. Time for pattern number 13. Again, a right angle triangle, so should not be a big issue. Uh, five rows, so we'll be running it for five times for sure. So let's go ahead and do it for pattern number 13. So void print pattern number 13 int n. Everyone knows what's the outer loop going to be? It's going to be something like, uh, from let's see, from one. Yeah, we know it's going to run for n times. Now let's look what's happening because it goes like one, and like two, then like three. Something for sure is for the first word, uh, we need one. For the secondary, we need two elements. For the third row, we need three elements. For the fourth row, we need four elements. So the fifth, fifth group, we need, uh, five elements. Something for sure is the out, the inner loop is definitely going to run for the number of row times. If it is first row, one time. If it is the second row, two times. If it is third row, three times. That is something which each of us know. But coming back, what am I printing? It's like one, two, three, four. It's a number which starts from one. So I know the printing is going to happen somewhere here. This is why we're going to print something for sure, and this is go where. So we know the number starts from one. So why don't we keep the number which starts from one, and you can print it something like this, and at every step, it's getting printed, we just increase it by one. Does this work? First time, one will come in, print. It'll go to the everyday. In the printing occurs, one, two, three, four. Quite simple, and this is pattern number 13 for you. So if I go ahead and say terminal, run task, and compile and run, this will actually run. It does. And if you want some clarity, probably we can give a space, and if you give a space, this is going to have much more clarity. Yeah, all right. So pattern number 13 done. Let's look at the pattern number 14. Again, a right angle triangle. So the outer loop is pretty, pretty, it's going to be till n. So what print pattern number 14 int n. And this ml goes zero based indexing. Again, you can go one way, zero based. The certain code will change as long as you're printing it, it doesn't matter. This is something which we will write. Now let's look at what are they printing it inside at every row, depending one, two, element trail. So number of rows equal to number, same as right angle triangle, but we are starting with A, and we are going till three. If it's a third row, starting with the A, we're going till 4. If it's the fourth row, that is something for sure. So can I see we can start with A? Now, actually, we can run, uh, something like character CH equal to A, and we can say CH lesser than equal to A plus of I, and you can do CH plus plus. Now, a lot of you might not understand. So it starts with A, and it says A plus I. So imagine I is something like two, it's basically saying, if I take you back to the iPad, it's basically saying, start with A, go till A plus, if I is two, A plus two. What does A plus two means in computer? What happens is AC is okay, two places ahead, BC. So let's actually see that loops from A. They'll see exactly. If you're saying A plus zero, it loops from A to A. It looks from A to A. And in ASCII, or in computer, it's stored like A, B, C, D. You can probably Google search about ASCIIs and you'll get a better idea. It's just too basic. Uh, so if you give C out of character, and he said this, and you go ahead and say C out of end L, let's see what happens. And I go over here, and I say terminal, run compiler. Well, this get printed? Yeah, it does, because the looping is done on the character this time. So, so the pattern number 14 is completed. Uh, time for the pattern number 15. It's similar to an inverted right, uh, angle triangle, which I think we have done in the pattern number five. Yes, we have. So it's, we just have to print like A, B, C, D, E, and then A, B, C, D. The first time it has to be, uh, for the zero row, it has to be n times. Then n minus 1 times, then n minus two times, then n minus three times, then n minus, uh, four times. So it's, it's pretty simple. So what I can do is, I, so what I can do is, I'll go over here. You know, the outer loop is definitely going to be something like I equal to 0, and I lesser than n, and I plus plus. And about the inner loop, it's something like character CH A, CH lesser than equal to. For the first time, if it is 0, if you see it prints all the five. It's like A plus five times. Is it no, four? Why? Because it's like, if I just write it, it's like A, B, C, D, E is what you're printing. So this is A plus 4. So it has to be A plus 4, not 5. Be careful about this. So it's like this is n. Next time it will be A, B, C, D. Such like A plus three. Got it. It's like A plus whatever n is minus this is the 0th row. This is the first row. N minus row minus 1. Please, pretty simple. Got it. Into the maths afterwards. It's like A plus n minus this, this, and then you can do a character plus plus. You can go ahead and do this, and then this, and then you can go ahead and do a C out of end L. This, once you've done this, go ahead and print this, and you will see that this is giving you a character. So with this, we have completed the pattern number 15 as well. Again, the pattern number 16 is a right angle triangle, but this time it's like A, B, B, C, C, D, D, and then Pi V. Is pretty simple. You start with A, and then you go to B, and then you go to C. It's like for the zero row, the character will be A. For the first row, the character will be B. Now, the third row, the character will be C. It's like A plus the row number, right, isn't it? Because for the first row, for the zero row, it's A. It's something like A plus I. If the first row is just P, it's something like A plus I, because A plus 1 will be B. Correct. So again, pretty simple. I'll just go ahead and write void print 16 int n. And then for int I equal to 0, I less than n, I plus plus. And the character that you will be printing will be definitely A plus I. And how many times in a right angled triangle? I don't need to say it anymore. Now everyone knows this. Now see out CH and then you go ahead, this, and then you go ahead of end L. Once, oh, my bad. It's like zero. Okay, once you've done this, you can go ahead and say pattern print 16. And this will get printed. Oh, sorry. I think A did not get printed. Why? Because this. Now you're good. A, B, B, C, C, triple D, triple E. Done. So a second number 16 is done. Let's look at the pattern number 17. The pattern number 17 is again similar to, uh, if you remember, I think, yeah, this one where you have space star, please. So it's like space, alphabets, space, but it has some cement. So you have to observe symmetry and then you have to print it. So the outer loop is again very straightforward. But with the outer loop, obviously 0, 1, 2, 3, 4. Without a loop, it's going to run for n times. The outer loop transfer n times. What are these spaces? Spaces, spaces, spaces, spaces, spaces. So it's like first, you have to figure out spaces, then alphabets, and then spaces again. So if you remember from the pattern number, as I said, it was this one, pattern number seven. Let's go to pattern number seven and look at the formula. So it was like the spaces was n minus I minus 1. So I can say those pieces as n minus I minus 1. That is something which we can copy. Probably let's copy this entire thing and paste it and slip pattern number 17. So instead of this, we have to somehow figure out the characters. Because as of now, we have printed the spaces, we printed the rows, but we have to figure out the characters. So if you go to pattern number seven, we know the number of characters are going to be C. That's 2 I plus 1. The number of characters are going to be 2 into I plus 1. That's for sure. Because this is one character, because this is one character, this is three characters, this is five characters, which is like two into two plus one. So we know the number of characters are C. Can I see? Can I say one thing for sure? If I am having five characters in this room, out of which, if I start with character A, what happens? If I have five, if I have five, symmetry of sub symmetry, if you observe symmetry, it breaks at half, right? It breaks at three. So can I see if it does five, five by two plus one, till this, I'll have A plus plus. I'll have A plus. Post this, I'll have A minus minus, isn't it? If I start with character A, I printed for whenever I am at the first time, whenever I'm at the first time, I print character, then I do A plus plus. Whenever I'm at the second time, it prints B, then I do A plus. Whenever at the third time, it's C, and then I start doing minus fine, so that it becomes B, and it gets printed at the fourth. Again, to A minus minus. At the fifth, it prints A. So can I just, can I just assign a character? I think I can. So what I'll do is, I will go and say, hey, can you please assign a character to yours? It'll be like, okay, let's do it. If you're assigning a character, can you go ahead and, you know, how many times you have to print that's 2 into I plus 1. That's the number of times you have to print. So let's at first print CH and see if it is getting printed correctly or not. It's pattern number 17. I'll go ahead and see. As of now, we have not decremented it. It's printing correctly. The space and everything is correct. If I do a character plus plus, and if I try to run it, it will be printed correctly, but it's getting increment. So I need to decrement it. So I know, I know what is the breakpoint. The breakpoint is this, whatever is the number of points, it's like 2 into this. These are the number of characters. Why two? Till here it increases. If you carefully observe, to carefully observed, it is 5. 5 by 2 is 2. Till 2, it increases positive. To decrease. So that in the next step, it is decreased. So can I see breakpoint will be this? So if your character J, your character J, again, uh, probably just to have a better understanding, you can just do it like this. One base indexing. If your character J is lesser than equal to the breakpoint, it's fine. Do a character plus plus. If it is not, then do a character minus minus. Quite simple. And now I'll go ahead and say, hey, can you now run and see if it is running fine? It is. This is running absolutely fine. So very simple. You just have to do a bit of mathematics, bit of observation, bit of symmetry, and the four rules that works. So we are done with one more part. Nice. Let's look at the next pattern, pattern 18. It's like E and D, E, C, D, E, then it's like B, C, D, E, and it's like A, B, C, D, E. So how do you do it? Probably pause the video and think it, because this is interesting. So that's right in the triangle again. [Music] Very easy. We're actually going from E. Is definitely the last one. And before this, we're going from E minus I, isn't it? What I'll do is, this is pattern number 18. So I'll go ahead and code void print 18. idn. And I know the outer loop is this. I know the inner loop will be from character CH will be E minus I. Character will definitely go until E. Character plus plus. And you can definitely print the character. And going ahead, you can definitely do an end L. So print 18 is what you have to do. And I'll go ahead and run this and see if it's running fine. It does. So I can say one more pattern done as well. Let's come to the next pattern, which is pattern number 19. Now, this is again an interesting pattern, but I guess you, as of now, have an idea how to do it. So if you carefully look at this, we see a symmetry. The symmetry is something, uh, like this. This isn't outside one, and this is the downside one. So let's first solve this one, and then we can solve the downside one. Okay, so I'll, I'll just be drawing the symmetry line. Some of the yellow line is what we will be solving at first, and then we can solve the bottom one as well. So above the yellow line, what are we printing? Let's look at this. We are printing stars, right? We are printing, uh, spaces, and again printing stars. And the outer loop is definitely very obvious. If ns5, that's the number of times the outer loop will run. Stars, spaces, stars. So that is something which you have figured out for the pattern number 16, sorry, 19. So you can go ahead and say IND. And we know that this is going to run for n times for sure. So we can be like, first we gotta print some stars, then spaces, then again stars. That's for us to look. How many stars, how many spaces, and how many stars? So if I look at this, for zero row, I'm actually printing five stars and five stars, which is for zero. I'm printing five, five, and zero spaces. For the first column, we're actually printing four, four, and two, depending. Four, four, and two. For the second column, we're actually printing three, three, and four spaces, isn't it? For the third, we are printing two and six spaces and two. For the fourth, so zero, one, two, three, four. Four, four, we are printing one, one, and one, two, three, four, five, six, seven, eight. That's easy. That is super easy, isn't it? What is this? It's like n minus I. So can I say the stars is n minus I? Number of stars is what I'm printing. I am n minus I is the number of stars and printing. And can I support spaces? We can start with 0 and we can keep on upgrading them with plus 2. As simple as that. So I'll go back to the code. And I know for stars is something like I and DJ equal to 1. And we just now figured out n minus I is what we are printing. So go ahead and say start. So we can just go ahead and Ctrl C. Stars is done. For spaces, we know, uh, initial spaces is nothing but zero. So you can go with the initial spaces. And there will be no command line int J equal to 0. And really J less than any space. And G plus plus. And you can go ahead and say for rather C out base. And once everything is done, my bad, once everything is done, you can just increase the spaces. Y equal to 2. And you can do a C out of end L. By the way, initial spaces, what you have to increase. Perfect. Once you've done this, this is pattern number 19. Let's see if this gets printed. The terminal run task component. Let's see. So if you try it, uh, so this will be J, by the way. That there was a type of four. If you try to run this, we will actually see that it's getting printed properly. The first thing is printed. Now let's look at the second. And that's again easy. That is again easy. So if I try to again write the star spaces combo, stars, pieces, stars, it's like one star. For again, we can, we can restart it. It's, we can just start it from one, two, three, four, five. It's like one star, one star, one, two, three, four, five, six, seven, eight spaces. There'll be six spaces, two stars, two stars, three spaces, four stars. You can just do the second half as well. Second half will one base indexing will be a better way. And then we know the initial space. We can again just initial space equal to eight is what we can keep. And then we can just copy paste this entire stuff because it's symmetrical. And over here, instead of n minus I, this will be exactly I. And this will be minus equal to 2. Because it's reducing by 2. I'll go ahead and try to run this and see if it is running fine. You see, so what I can say is the pattern 19 is also completed. Now it's about the next pattern, which is the pattern 20. So can I see over here? It's again the same pattern, but this time, can you break down into symmetry? You cannot, because there is just, if I, if I carefully show you, there is this one line over here. You could, because it was exactly symmetrically opposite, but this is not symmetrically. There is just a single line. Had this line been twice, we could have done it accordingly. But no issues. Maybe we can figure out a way and we can do it, because these are nothing but similar to right angle time, isn't it? Similar to writing. So we will first have a look in over here. Assume it's fine. It's like one, two, three, four, five, six, seven, eight, nine. Something I know for sure is 2 and minus 1 times is what the outer loop will run. 2N minus 1 times is what the outer loop it run. This is something for sure. So if I look at this, we're actually printing like one star, a lot of spaces. How many? Let's count. One, two, three, four, five, six, seven, eight spaces. To start of it, one star, eight space, one star, two star, two star, six piece, three star, three star, full specs, four star, four star, two space, and then five star, zero space, five star, and then post our poster. So can I see after, can I say after the fifth row, can I say after the fifth row, there will be a subtle change? The moment you are at the row five, or maybe n by 2. The moment you are at the row n by 2, on the next step, there will be a change. And the change is very subtle. This, the space will increase. Till here it was decreasing. The space was decreasing, but whenever we reach here, the space, the space starts to increase. The space will start to increase, right? And what happens to the rows? You're just printing four, then three. We had done this. Yes, we have done this. If we go ahead, we have done this in the pattern 10. So we'll go ahead and look at the pattern 10. How are we doing it? We were saying the stars will be 2N minus I. The moment it crosses the nth guy, right? So this is what people do over here as well. The moment it crosses this guy, it's going to be nothing but the number of stars. Till here it was very simple. Whatever is the rule number, that's the start. At the moment it crosses, it will be 2N minus I. Again, very obvious. For the six, two into five minus six, that's four stars done. So I have figured out all the formulas, everything. Now it's time to go and quickly code this up. It's like void print 20 int n. And I know one thing for sure, this is going to run till 2 into n minus 1. And I plus plus. That's something which we know. Now, what are we printing? Stars at first, spaces, and then steps. Let's print the stars. Either stars, the number of stars are very simple. The number of stars depends like it will be rows. But if, if the eye has crossed the N, then the stars will be through. In the sense, I stars will be 2 into n minus the root number. You can just print the stars now. For INT J equal to 1, we can go ahead and print those many stars. And we can go ahead and do J plus plus. And C out of star. That's it. And over here, you can just copy B is the same thing. And you can see this. So this will print all the stars. What about the spaces? You know those pieces, how they increase. So maybe you can keep the spaces initially as, if you remember, the initial space was eight. So instead of keeping it as eight, you can say 2N minus two. I think we did a mistake here by keeping the space as initial space as, okay, it was right. It was zero. This will not be eight. Instead of this, this will be 2 into n minus 2. My bad. I did a mistake there. But, uh, nevertheless, so, okay, 2N minus 2 is the initial spaces that we start off with. And then while going, we can do like J equal to 1. And we need these many spaces. So J plus plus. And you can just go ahead and say space. At the end of the day, you can see out and L. And if I is lesser than equal to like lesser than, sorry, lesser than n, then you say spaces will be increased by two. L spaces will be decreased by two. This is what you will do to the spaces. And this is pattern number 20. So let's go ahead and print this and see if it is running fine or not. Okay, we have an issue. Looks like there was an issue while printing it. Why did it happen? Let's quickly have a look. And this is where the space increases. Uh, okay, let's run it quickly. Let's compile and see. Look at this time. It's both. So, so another pattern done, isn't the pattern printing super easy? So we're done with the pattern number 20. We will start with the pattern number 21. The image is wrong. I'll get it fixed. The correct image is this, because you have to print a square. So if you look at the pattern, a given four, so it's a four size square. So for a square, you know the number, the outer loop is very easy. Outer loop is going to print for n times. That is for sure. But if I look at the square, it will stars. Where does it have stars? Obviously in the boundaries. That is easy. Fill the stars in the boundaries only. Can I say I'll run for all the time? I'll run for all the times. I'll run for all the times. That means I'll run for four times. Every time I'll run for four times. Every time I start from something like 0 and then go on till one. I'll run it. And then I'll have a loop which again runs from 0 to n. I'll have a loop. I transfer 0 to n. So can I see this that I'll just fill the boundaries? And if I try to, if I try to write the index six, let's write the indexings. It's very simple. But did you write down the indexing? It's like 0, 1, 2, 3. 0, 1, 2, 3. So can I see whenever I is 0, which is this, or whenever I is equal to n minus 1, which is this, or whenever J is 0, or whenever J is n minus 1, can I say these are the cases of the boundaries? These are the cases of the boundaries, because whenever I is 0, it runs J runs 0, 1, 2, 3. Whenever I is 1, J runs 0, 1, 2, 3. Whenever I is 2, J runs 0, 1, 2, 3. Whenever I is 3, J runs 0, 1, 2, 3. So can I say these are the cases when we know we have to fill the stars? As simple as that. So, so what are you, so what are you waiting for? Let's go ahead and print this pattern. It's like void print 21 idn. And then you say, okay, I know I have to print these many. I and DJ equal to 0. And then you say, hey, listen, I know if you're this, or he does, or URLs, or the others, then only I'll print a star. If not, then I know you will have a space. And over here, I can say handle. As simple as that. And I can say 21. Run task. Let's run and this is this will be space. Uh, now let's run it quickly. Run it and see if it is running fine. Yeah, yes, it's printing a square of the size 5. So what I can say is, I'm done with the pattern number 21 as well. The last pattern. Finally, my, my voice is giving up now. So the first step that I will do is again, it's very observation. It will not come to you for the first time. So to not worry if it doesn't comes to your head. So what I'll do is, I know there are seven rows and I know there are seven columns for something like n equal to 4. So what I will do is, at first, I will subtract 4 from every value and I'll get a new matrix. The new matrix will be like 0, 0, 0, 0, 0, 0, 0, and then 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0. So once I have subtracted 4 from everything, like 4 minus 1, this gives you 3. 4 minus 3, this gives you 1. So you just subtract 4 from every value. So let's solve this. And once you get the answer, you again subtract 4 to get a very standard process. So what I've done is, I have subtracted n from it, which is nothing but the four, the value to get the new matrix. Okay, once you get this new matrix, you can again subtract the new matrix to get the value. Right? It's, it's nothing but like this was the current value. This is the current value. And this is the new matrix. So again, if you just take it to the other side, it'll be n minus new matrix equal to current value. Pretty, very obvious. Pretty obvious. So I need to generate this. Generating this is very easy. Let's understand how. So I know in the matrix, it's like 0, 1, 2, 3, 4, 5, 6. 0, 1, 2, 3, 4, 5, 6. 2D Matrix back step 1.1. I've taught you to demand. This is how the indexing goes like seven rows, seven columns. So the outer loop runs for seven times. The inner loop runs for seven times. This is something very sure because we've got to print seven, seven. How is seven related to four? Very, very standard math. 2 into n minus 1 is how it is related. So if I go back to the code, I know the inner, I know the outer loop, which is 200 minus 1. I know, sorry, it'll go from 0 to 2 into n minus 1. And I plus b. And I know the inner loop will go from 0 to 2 into n minus 1. And G plus plus. This is something we know for sure. Now let's have some observation. What is this value? Something to observe is, if I take this, the distance is 1. The distance is 2. The distance is 5. The distance is 4. So you take always the always the minimal distance, which is 1, and you place it here. Let's take one more example. Three. The distance is three, three, three, three. If you take the minimal from this portion, when I say this portion, this portion is nothing but the row number, three, the column number, three. And in order to find it, you find the top distance, left distance, right distance, bottom distance, and you take the minimal of it. And once you take the minimal, you actually find this value. This 2 is nothing but the distance from the bottom. This 1 is nothing but the distance from the bottom. The 0 is nothing but the distance from the top. You have to find the distance. And you know how to find the distance. It's very, very simple. So if it, if I talk, uh, my bad, if I talk about this guy, what's this distance? It's nothing but the row number. So can I say the top distance is nothing but the row number? I can. Can I say the, uh, left is nothing but the column number J? Why? Because if I'm talking about this, this 3 is the distance, isn't it? And if I talk about the right, what's right? If I'm standing here, this is the distance. It's like six. The last index is definitely 2N minus 1 minus 1 is the last, like seven minus 1, 6 is the last index. And then in order to find the distance, whatever is G. And the bottom, similarly, the last index is 2N minus 1 minus 1 minus I. Why this distance? If this is the case, we take this 6. And then you might. So once you have got this, you can actually get all the values. Once you've got all the values, you subtract n, and you get your answer. So we know one thing for sure, uh, the top distance is I. The left distance is the column G. And we know the bottom distance or the right distance is 2 into n minus 1 minus 1, which is minus 2. Subsequently, minus of the column number. Okay? And we know the bottom distance or the down distance is 2 into n minus 2. That's the last index minus I. And once you've got this, you know what you have to print. The value will be subtracted by n, because previously you also subtracted and the Min of all of these. So Min of top, down, and mean of left, right. The minimum function only accepts two variables, that's why. And you can do a end L. Is it right? Okay, let's, let's go ahead and see if it is running fine. Yeah, it is. It does. Very easy, isn't it? So we have completed 22. And all the patents are finally completed. So guys, I hope you've enjoyed the entire lecture. It took everything out of me. So please consider hitting that like button. And to follow our ritual, do comment understood so that I get an idea that you understood everything. If you have any doubts, put it into the comment section. I will be replying back, or the community will definitely help you. If you're new to our channel, what are you waiting for? Hit that subscribe button. Follow strivers A to Z DSA course. Share your learnings using the hashtags strivers A to Z DSA. And yeah, with this, I'll be wrapping up this video. Let's meet in the next lecture. Till then, bye-bye. Take care. [Music]