# Lecture 4: Solving Pattern Questions (Part-2)

https://www.youtube.com/watch?v=dr-pLeJBr38

[00:03] Hello ji, how are you all?
[00:03] This is Love Babbar and welcome to the channel CodeHelp.
[00:07] In the last video, we studied about the if else and while loop.
[00:07] We also did 2 pattern questions in that video.
[00:13] If you haven't understood that 2 questions, no need to worry, we are going to solve 18 pattern questions in this video.
[00:19] So let's get started with our first pattern question.
[00:22] Let's say I want to make this pattern.
[00:35] Observation- Rows = 4, Columns are starting from 1 and going till 'n'.
[00:59] So we for the row part, we can run a while loop from 1 to n.
[01:05] And inside this loop, we will make an another while loop, that too from 1 to n.
[01:12] You can notice here, that we have to simply print our 'J' value to get our answer.
[01:30] So here is our rough code: J=1, while(j<=n) {cout << j; j=j+1;}.
[01:43] Then we will print new line at the end of this loop and increment i.
[01:49] Let's understand this one again.
[01:54] Here is the pattern which we have to print.
[01:54] You can notice here that we are just printing the column number.
[02:32] If there would be a pattern something like this.
[02:32] Here we can notice that we are just printing n-j+1.
[03:06] We just have to observe and find this particular expression which will tell us what we have to print.
[03:26] I will write code for this one.
[03:26] This reversed one is your homework.
[03:40] Let's code it.
[03:42] First we declared 'n', then we are taking its input from the user.
[03:47] Initializing i with 1 (i represents row number).
[03:51] Then we ran a while loop from 1 to n.
[03:53] Then we will declare an another variable which will represent column number.
[04:01] We can run one from while loop from j=1 to n, and simply keep on printing column number.
[04:23] Then outside this column while loop, we printed a new line and incremented 'i'.
[04:34] This is our code.
[04:34] Now let's run it.
[04:38] I entered 6 and it gave me the output.
[04:44] What if I make a small change here.
[04:47] I will change cout << j; to cout << n-j+1;
[04:54] Let's run and check what will happen.
[04:56] Now I entered 6 and it printed it in the reverse order.
[04:59] See, how easy it is.
[05:06] Okay, one more question.
[05:11] This is the pattern which we have to print now.
[05:18] Can you observe any relation here?
[05:19] No?
[05:40] Now how to solve this.
[05:42] We are not able to find any relation but we know that it is simply printing the counting.
[05:56] So we can make a variable count, print it and then increment it.
[06:06] Let's try to try this approach.
[06:13] First I am traversing on all rows and columns using the same method.
[06:32] Now let's take a variable count=1.
[06:41] And we will print this count and then increment it.
[06:48] Oh we have to increment J also, let's do that.
[06:54] Outside this loop, we will enter a new line and increment 'i'.
[07:07] The code is ready, let's run it.
[07:11] See, it is giving correct output for 5.
[07:25] It is not looking good, let's add space.
[07:30] See, now it is looking nice.
[07:42] Bhaiya, now I can solve any question.
[07:42] Really?
[07:42] Then try this one.
[07:49] This is the pattern which we have to print now - Triangle pattern.
[07:58] We know that row = n.
[07:58] Also we can notice that number of stars in a particular row is equal to the row number which is 'i' here.
[08:34] So the column loop will run till 'i' instead of 'n'.
[08:41] Let us write the code for this approach.
[08:51] Took input 'n'.
[08:54] First I am traversing on all rows.
[09:13] Then inner loop will run from 1 to i.
[09:42] Here inside this col while loop, we are simply printing a star.
[09:50] Then incremented col, added a new line outside the col while loop and then incremented 'row' too.
[10:04] We are done with the code.
[10:04] Let's run it for 4.
[10:09] You can see, we got our output.
[10:14] Got it right?
[10:18] Next pattern.
[10:23] This is the pattern which we have to print now.
[10:30] Row = n.
[10:32] 1st Obs - Number of elements being printed in each row is equal to the row number.
[10:49] 2nd Obs- We just have to print the row number.
[11:08] Very easy question.
[11:08] Let's code.
[11:12] First I am traversing on all rows and columns using the same method which we did in the last question.
[11:34] This time instead of printing the star, we have to print the row number.
[11:40] Then incremented col, added a new line outside the col while loop and then incremented 'row' too.
[11:52] First we traversed to each row.
[12:05] And in each row, print 'row' elements, and keep on printing the row number.
[12:28] Let's run it.
[12:28] Oh we did some mistake I guess.
[12:44] Oh we missed a sign here.
[12:44] Let's run it again.
[12:54] Input = 6.
[12:58] We got our output.
[13:01] You can see, observation plays an important role in these pattern printing questions.
[13:05] This is the pattern which we have to print now.
[13:12] Bhaiya, I know this one :)
[13:14] We have to run while loop from 1 to row_number for each row and simply print the count.
[13:38] This is your today's homework question.
[13:42] Next pattern is.
[13:51] This is different from the last one.
[14:00] 1st Obs - Row = n.
[14:12] So we will run the outer loop from 1 to n.
[14:19] 2nd Obs - The number counting in each row is starting from the row number.
[14:35] Second loop will run from 1 to 'row number'.
[14:53] We know that we have to print counting numbers starting from row number, but how to write this in code?
[15:11] So we will declare a variable inside this row loop but outside the col loop, then after printing it inside col loop, we will keep on incrementing it by 1.
[15:27] There is an another method for this question.
[15:30] But first let's write the code for this one.
[15:34] We can't simply print row and increment it, because then our program will misbehave.
[15:49] We can read the row number but we can't write to it because it can disturb the whole flow of the code.
[15:58] First I am traversing on all rows and columns using the same method.
[16:30] Now what we have to print?
[16:35] For that, we will declare a variable 'value' here.
[16:35] It will start from the row number.
[16:43] And then we will print and increment it.
[16:51] Then incremented col, added a new line outside the col while loop and then incremented 'row' too.
[17:02] Done.
[17:03] Let's run it.
[17:06] 5.
[17:08] See we got the output.
[17:15] We just need to think about the upper limit of row number and column number, and the value which we have to print.
[17:24] There is an another method to solve this question.
[17:29] You have to think, can we solve this question without declaring this extra variable?
[17:39] This is your homework.
[17:42] If you will solve it, then only you will become a champ.
[17:48] It's on you whether you want to be a champ or not.
[17:57] Next pattern.
[18:02] Printing in the reverse order.
[18:04] 1st Obs - row =n.
[18:14] You can see that inner loop is running 'row' times.
[18:37] Now what we have to print.
[18:43] You can notice that numbers being printed are starting from 'row number'.
[18:50] But we have to use the value variable if we want to print this pattern.
[18:50] What if we print it without using any extra variable?
[18:58] Bhaiya, isn't it similar to the a question in which we printed numbers in reverse order?
[18:58] Yes you are right.
[19:06] What if I do this: n-j+1?
[19:06] What will happen?
[19:22] It is not giving correct output.
[19:22] Then how to correct this?
[19:38] What if I print i-j+1?
[19:45] Yes, it is giving correct output for every position.
[20:37] So we got our formula = i-j+1.
[20:41] I just tried hit and trial method to get this formula.
[20:46] Now let's code it.
[20:47] After taking input, I initialized 'i' with 1.
[20:53] And then I traversed over each row.
[21:00] Inside this loop, we know that we have to go till the row number.
[21:20] Then we have to print i-j+1.
[21:31] Then incremented j, added a new line outside the col while loop and then incremented 'i' too.
[21:42] Let's run it for 3.
[21:45] It gave correct outptu.
[21:47] For n=4 also, it gave correct output.
[21:51] Let's try a bigger number, n=15, again it gave us the correct output.
[22:00] Let me add space.
[22:07] First we observed something and then found a formula by Hit and Trial.
[22:13] This is how we solve pattern problems.
[22:18] We have done 9 pattern questions.
[22:21] Let's do some more questions to understand this topic completely.
[22:33] Next pattern looks like this.
[22:51] 1st obs - row = n
[22:56] 2nd obs- A is mapped to 1, B is mapped to 2 and so on
[23:16] This time we will run both loops from 1 to n because it is a square pattern
[23:48] Now we have to figure out, what we have to print here
[23:58] We can't make a relation depending upon the column number because value is not consistent in a column for different rows
[24:38] But we can try out for rows. Let's try it out
[24:44] what if I say that you have to print ( char ch = 'A' + i -1 )
[24:55] Did you get this?
[24:57] Let's replace 'i' with row
[25:11] Let's check it for first row
[25:16] It is giving 'A'
[25:23] If i=2, it is giving us 'B'. So it means that it is working absolutely fine
[25:35] This is our relation = 'A' +i -1, where 'i' is the row number
[25:50] Let's run it
[25:54] Took input, First I am traversing on all rows and columns of this square pattern using the same method
[26:17] Then we have to print 'A' + i - 1
[26:27] Then incremented col, added a new line outside the col while loop and then incremented 'row' too
[26:42] Done. Let's run it for 5
[26:56] Bhaiya what is this. It is converting character to 'int' after after performing this calculation here but we don't want this
[27:09] So let's first store it in a char, so that i gives correct output
[27:14] Now it is working fine for various inputs like 5, 3
[27:42] Got it? Now let's move on to the next question
[27:51] No try this one
[27:56] I will call this as row or 'i'
[28:06] and will call upper one as col or 'j'
[28:12] You can see that the printed number is depending on the col.
[28:18] Now this is the relation which we will write= 'A' - j +1
[28:26] Done, simple
[28:28] Let's check whether we are getting correct values or not
[28:41] We are getting correct answers. Now you yourself have to write the code for this one. Entire code is similar to the last one, just the formula is changed
[28:51] This is your homework
[28:55] Do this now
[29:06] Simple. Observation - Simply counting is there(in terms of character)
[29:12] I will take a start variable ='A' and then print it and increment it to 'B' and so on
[29:32] It is similar to that counting question
[29:38] There we stored a integer value, but here we will store a character. Done
[29:50] This is also your homework
[29:54] Let's do this one
[30:02] It is similar to this question which we did earlier. It is very easy
[30:12] Now what should be our relation, as it is depending on both 'i' and 'j'
[30:21] Let's write row and col number first
[30:35] What if I do i+j-1, let's try
[30:47] We will convert this 1 into 'A' later, but first let's check out the values
[31:23] So we have got our relation as it is giving correct output for all positions. Now I have to find a relation to convert these integer values to characters
[31:32] What if I add 'A'-1 on both sides, let's try. See we got our formula
[32:20] Bhaiya how you calculated this, its very difficult. No it is not, see.
[32:24] We got our counting correct, but just had to map 1 to 'A', so to do so, we added 'A' -1 on both sides,
[32:53] Now the code is same, we just have to change the formula.
[33:00] First I am traversing on all rows and columns using the same method - Square pattern method
[33:14] then here we will simply print = 'A' + i + j - 2
[33:24] Then incremented col, added a new line outside the col while loop and then incremented 'row' too
[33:36] Simple
[33:38] You have to implement it by yourself
[33:49] Let's try this triangular one, but with characters this time
[33:56] 1st Obs - Printed value is depending on the row number
[34:07] 2nd obs - Number of characters in each row = row number
[34:19] So first we will run a loop from 1 to n for traversing to all rows
[34:33] And inside this loop, we will run the col loop till the row number
[34:45] Now let's figure out what we have to print. We can see that we have to map the row number with characters somehow
[35:03] Can I write = 'A' + row -1
[35:12] Trying this formula for different positions
[35:36] It is working fine, so this is our formula
[35:40] So we will print 'A' + row-1.
[35:42] Then incremented col, added a new line outside the col while loop and then incremented row too
[35:50] Let's code it and run
[35:52] Took input of 'n'
[35:56] So first we will run a loop from 1 to n for traversing to all rows
[36:05] And inside this loop, we will run the col loop till the row number
[36:24] I have to print 'A'+i -1, but it will be typecasted to integer, so to avoid that, I will put this value inside a character and then print it
[37:05] Then incremented col, added a new line outside the col while loop and then incremented row too
[37:14] Let's run it for 5
[37:18] Oh, we have to write 'row' here inplace of 'i'
[37:25] See we got our output
[37:30] We just have to figure output the limits of this row and col number , and we have to find a relation for printing our answer
[37:48] See this one
[37:59] It is simple counting. I will initialize a variable with 'A' and increment it after printing. Simple
[38:18] It is similar to these 2 questions which we did earlier. If you are not able to do this question by yourself, then revise these first
[38:38] Let's increase the difficulty level
[38:45] Bhaiya, this one is easy
[38:54] 1st obs - row = 1 to n 2nd obs- Col = 1 to row because it is a triangular pattern
[39:24] It is depending on the row number. Let's find out the relation
[39:30] Can I do this, 'A' + row -1
[39:33] It is working fine for 1st element
[39:38] Let's check for this 'D'
[39:41] Oh it is giving wrong output, what to do now
[39:56] With this formula, we will get answer same for each row, so what changes we can make
[40:09] Should I include 'j' also in my formula? Oh yes, it is changing with 'j' too
[40:20] Should we try this one = 'A' + row + col -2
[40:30] It is similar to this question
[40:36] Let's try to get this 'D' by using our formula
[40:56] see we got 'D'
[40:58] Let's do for this 'G'
[41:01] We got 'G'
[41:21] So this is our formula
[41:24] So first we will run a loop from 1 to n for traversing to all rows and column will go from 1 to row, formula is 'A' + row + col -2
[41:36] I will show you the code
[41:39] I will just make few changes here in this code
[41:45] See it is giving correct output on running
[41:50] Let's move to the next question
[41:56] Now try this one
[42:04] We can see their dependence on row number and col number
[42:21] We know that it is written in the reverse order. Also 'A' will be there for sure
[42:37] Now what should we add in it to get 'D'
[42:41] Can we add J? No, then we won't get this C
[43:03] Let's find something else
[43:05] What if I do something like this, n-i-j+1
[43:15] We are getting answer for 'D'
[43:29] Let's try for this 'D'
[43:35] No we are getting wrong answer here
[43:57] Now what to do
[44:02] What if I just find the starting character and then keep on incrementing it till the end of the row
[44:17] What if I do something like this, to find the starting character
[44:25] For first row, we are getting correct answer
[44:31] Let's try out for 2nd row. Again we got correct answer
[44:38] So we came to know that the starting character of every row is 'A' + n - i
[44:48] Then we just have to increment it
[44:52] Okay, we got the logic. Now let's write the code for this one
[44:56] Took input
[45:04] So first we will run a loop from 1 to n for traversing to all rows and column will go from 1 to row because it is a triangular pattern
[45:27] Now let's make a starting character
[45:36] start = 'A' + n - row
[45:46] we will then print this starting character here
[45:50] After this we will increment it by 1
[46:09] Then incremented col, added a new line outside the col while loop and then incremented row too
[46:22] Oh sorry, I used i inplace of row, let me fix that
[46:36] See we got our output
[46:46] It was very easy question. We just have to find the starting character. After that it became very simple
[46:50] Can we simplify this formula which we made for this question which we did earlier
[47:11] This is your homework. Do this one
[47:30] Now let's try out this variant. It includes spaces too
[47:43] You can see that before printing these characters, we have to print some spaces too
[47:50] Inshort, we have to use two spaces, one for printing space and other one for character
[48:00] see, it is looking similar to square matrix
[48:06] You can see that numbe of spaces = n-i
[48:47] So one loop will run for space time, and it will just print space
[49:06] Now, after then we have to print a character. you can see that number of characters = row number
[49:16] So we will print star inside this another while loop
[49:30] Then we entered a new line, increment row and boom, we are done with our code
[49:42] Now let's code it and run
[49:45] First took our input, then we will traverse over all rows using this while loop
[50:10] First I will print spaces
[50:17] and then I will print stars
[50:22] For spaces we can do something like this
[50:41] Now let's print stars
[50:42] We will run a loop (row) times and keep on printing stars
[51:07] Then we will end the line and increment row
[51:15] Again, I used i inplace of row, let's fix it and run it
[51:24] see, it gave correct output for 4
[51:28] Now you can also solve these types of questions easily
[51:41] If this was the question, then we don't need to print space. We just have to print the character and Tada, we are done
[51:58] Here the number of characters in each row are equal to n-i+1
[52:21] See it is giving correct output
[52:36] So star = n-i+1. Very easy, right
[52:45] You have to write code for this. It is your homework. If you will complete homework questions, then only you will improve
[52:54] If the same question would have been given like this, then you need to print the spaces too
[53:06] We can see that spaces are increasing as the row number is increasing. So No. of space = row number-1
[53:25] Stars are same as above question. Stars = n-i+1
[53:37] This was also easy question
[53:41] Write code for this as your homework
[53:46] Can I make something like this
[53:56] This is also your homework
[53:59] How to solve this one?
[54:09] This is also your homework
[54:13] Now let's see this one
[54:24] This one is also your homework
[54:28] One another question. This is also your homework
[54:36] These are very easy questions if you have understood previous questions
[54:43] Now let's try a hard question
[55:01] We can see that there are three components - Spaces, a triangle and one another triangle
[55:23] If you are able to do this one without my help, then it is very good for you.
[55:51] But if you are not able to do, then no need to worry, I will tell it's solution and then give you one more question to try as your homework
[55:58] 1st obs - Spaces = n-row number
[56:33] This inner triangle is similar to this question which we did earlier
[57:02] We are simply printing J, see how easy it is
[57:07] Now see this triangle
[57:11] But before that, let's print the other two things, then we will come to it
[57:19] First we took the input n
[57:25] We will start from 1 and go till 'n'
[57:32] We have to print 3 things - Space, 1st triangle, 2nd triangle
[57:47] For the first part we will do something like this
[57:55] Spaces = n-i
[58:00] and then run a loop space times
[58:04] We will simply print space and then decrement it
[58:10] Now to print the triangle, we simply have to print J
[58:15] We will run a loop from 1 to row(because it is triangular)
[58:29] Then we will print J and increment it
[58:37] Let's run for this one and see what will happen
[58:54] For 4, it gave us this output which is correct, but we have to print the 2nd triangular pattern
[59:03] Let me change these things here
[59:15] See, try to find it's relation with this row number. Got ?
[59:43] The first letter is starting from i-1
[59:54] and after that we are simply decrementing it
[01:00:00] see, start = i-1
[01:00:09] then we will Simply print the integers in reverse order
[01:00:24] Let's run it now
[01:00:36] See it gave correct output for 4
[01:00:38] There may be some other methods to solve these questions. We can do with anyone
[01:00:51] Now you are champ in pattern questions. Complete that homework questions too, then you will not face any problem in these types of questions
[01:01:01] Now do this hard question, if you are able to do it, then you have become a champ in patterns
[01:01:44] It is known as Dabangg pattern
[01:01:49] This one is for n=5. It is very easy if you will do it by diving it into different sections
[01:01:58] I will give you the hint
[01:02:02] I can do it by dividing it into these sections
[01:02:14] You may have some other approach. This one is your homework
[01:02:22] So we have covered almost everything, if else, nested ifelse, loops, conditionals,
[01:02:29] We did prime numbers, printed numbers from 1 to n, then calculated sum of all even numbers from 1 to n, did while loop, did more than 20 questions on patterns
[01:02:48] I myself have implement 14-15 questions and rest questions are given as a homework to you
[01:02:55] If you will not implement these by yourself or not do homework, then you will not get anything
[01:03:02] This session's length is over 3 hour now, but length doesn't matter if you enjoyed it because that it what matters the most
[01:03:11] This was for today's video. In next one, we will see operators, bitwise operators,
[01:03:17] For loop, and many more interesting things
[01:03:24] Now we are comfortable with variables, data types, conditionals, loops, so we can start practicing questions too
[01:03:35] You should finish your homework. Also tell the answers in comment section for the questions which I asked inbetween of the video
[01:03:50] So this one was for today's video. Will see you in the next one. Till then, Bye
