Lecture 4: Solving Pattern Questions (Part-2)

Lecture 4: Solving Pattern Questions (Part-2)


Lecture 4: Solving Pattern Questions (Part-2)

In this Video, we are going to solve a lot of Pattern questions.

This video is made in continuation with Part -1 [   • Lecture 3: If-Else, While loop \u0026 Lots…   ], Please watch that before watching this.

There is a lot to learn, Keep in mind “ Mnn boot karega k chor yrr apne se nahi yoga ya maza nahi para, Just ask 1 question “ Why I started ? “


Discord Server Link: https://discord.gg/feSQvVXMrd
Course Flow: https://whimsical.com/dsa-4-placement
Homework: Added in Video already
Homework Timestamps [13:20, 17:09, 27:58, 29:28, 33:30, 37:25, 41:40, 51:30, 52:35, 53:35, 53:45, 54:10, 01:00:43]

Handwritten Notes: https://drive.google.com/file/d/1onLk

Slides Link: https://drive.google.com/file/d/1IVwp

Code Links: https://github.com/loveBabbar/CodeHel

Telegram Group Link: Love Babbar CODE HELP
https://telegram.me/lovebabbercodehelp


Do provide you feedback in the comments, we are going to make it best collectively.

Connect with me here:

Instagram: https://www.instagram.com/lovebabbar1/
Twitter: https://twitter.com/lovebabbar3


Intro Sequence: We have bought all the required Licenses of the Audio, Video \u0026 Animation used.

#DSABusted


Content

3.1 -> Hello ji, How are you all? This is Love Babbar
5.44 -> and Welcome to the channel CodeHelp
7.232 -> In the last video, we studied about the if else and while loop. We also did 2 pattern questions in that video
13.694 -> If you haven't understood that 2 questions, no need to worry, we are going to solve 18 pattern questions in this video
19.762 -> So let's get started with our first pattern question
22.393 -> Let' say I want to make this pattern
35.384 -> Observation- Rows = 4, Columns are starting from 1 and going till 'n'
59.929 -> So we for the row part, we can run a while loop from 1 to n
65.196 -> and inside this loop, we will make an another while loop, that too from 1 to n
72.255 -> You can notice here, that we have to simply print our 'J' value to get our answer
90.093 -> So here is our rough code. J=1, while(j
103.451 -> Then we will print new line at the end of this loop and increment i
109.93 -> Let's understand this one again
114.1 -> Here is the pattern which we have to print. You can notice here that we are just printing the column number
152.131 -> If there would be a pattern something like this. Here we can notice that we are just printing n-j+1
186.453 -> We just have to observe and find this particular expression which will tell us what we have to print
206.27 -> I will write code for this one. This reversed one is your homework
220.018 -> Let's code it
222.33 -> First we declared 'n', then we are taking its input from the user.
227.762 -> initializing i with 1 (i represents row number)
230.984 -> Then we ran a while loop from 1 to n
233.913 -> Then we will declare an another variable which will represent column number
241.351 -> We can run one from while loop from j=1 to n, and simply keep on printing column number
263.011 -> Then outside this column while loop, we printed a new line and incremented 'i'
274.745 -> This is our code. Now let's run it
278.535 -> I entered 6 and it gave me the output
284.914 -> What if I make a small change here. I will change cout < j; to cout < n-j+1;
293.968 -> Let's run and check what will happen
296.411 -> Now I entered 6 and it printed it in the reverse order
298.994 -> See, how easy it is
306.454 -> Okay, one more question
311.289 -> This is the pattern which we have to print now
318.112 -> Can you observe any relation here? No?
340.529 -> Now how to solve this
342.867 -> We are not able to find any relation but we know that it is simply printing the counting
356.4 -> So we can make a variable count, print it and then increment it
366.037 -> Let's try to try this approach
373.926 -> First I am traversing on all rows and columns using the same method
392.113 -> Now let's take a variable count=1
401.061 -> and we will print this count and then increment it
408.866 -> Oh we have to increment J also, let's do that.
414.239 -> Outside this loop, we will enter a new line and increment 'i'
427.385 -> The code is ready, let's run it
431.593 -> See it is giving correct output for 5
445.528 -> It is not looking good, let's add space
450.381 -> See, now it is looking nice
462.088 -> Bhaiya, now I can solve any question. Really? Then try this one
469.065 -> This is the pattern which we have to print now - Triangle pattern
478.559 -> We know that row = n. Also we can notice that number of stars in a particular row is equal to the row number which is 'i' here
514.257 -> So the column loop will run till 'i' instead of 'n'
521.931 -> Let us write the code for this approach
531.073 -> took input 'n'
534.917 -> First I am traversing on all rows,
553.48 -> Then inner loop will run from 1 to i
582.686 -> Here inside this col while loop, we are simply printing a star
590.329 -> Then incremented col, added a new line outside the col while loop and then incremented 'row' too
604.552 -> We are done with the code, Let's run it for 4
609.8 -> You can see, we got our output
614.114 -> Got it right?
618.766 -> Next pattern
622.986 -> This is the pattern which we have to print now
630.732 -> Row = n
632.545 -> 1st Obs - Number of elements being printed in each row is equal to the row number
649.458 -> 2nd Obs- We just have to print the row number
668.479 -> Very easy question. Let's code
672.878 -> First I am traversing on all rows and columns using the same method which we did in the last question
694.321 -> This time instead of printing the star, we have to print the row number
700.678 -> Then incremented col, added a new line outside the col while loop and then incremented 'row' too
712.836 -> First we traversed to each row
725.581 -> and in each row, print 'row' elements, and keep on printing the row number
747.975 -> Let's run it. Oh we did some mistake I guess
764.752 -> Oh we missed a sign here. Let's run it again
774.67 -> Input = 6
778.541 -> We got our output
781.299 -> You can see, observation plays an important role in these pattern printing questions
785.183 -> This is the pattern which we have to print now
792.046 -> Bhaiya, I know this one :)
794.691 -> We have to run while loop from 1 to row_number for each row and simply print the count
817.958 -> This is your today's homework question
822.685 -> Next pattern is
831.919 -> This is different from the last one
840.074 -> 1st Obs - Row = n
852.859 -> So we will run the outer loop from 1 to n
859.733 -> 2nd Obs - The number counting in each row is starting from the row number
875.548 -> Second loop will run from 1 to 'row number'
893.339 -> We know that we have to print counting numbers starting from row number, but how to write this in code
911.56 -> 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
927.25 -> There is an another method for this question
930.57 -> But first let's write the code for this one
934.323 -> We can't simply print row and increment it, because then our program will misbehave
949.438 -> We can read the row number but we can't write to it because it can disturb the whole flow of the code
958.875 -> First I am traversing on all rows and columns using the same method
990.386 -> Now what we have to print
995.582 -> For that, we will declare a variable 'value' here. It will start from the row number
1003.037 -> and then we will print and increment it
1011.731 -> Then incremented col, added a new line outside the col while loop and then incremented 'row' too
1022.794 -> Done
1023.751 -> let's run it
1025.996 -> 5
1028.925 -> See we got the output
1035.076 -> We just need to think about the upper limit of row number and column number, and the value which we have to print
1044.862 -> There is an another method to solve this question
1048.982 -> You have to think, can we solve this question without declaring this extra variable
1059.886 -> This is your homework. If you will solve it, then only you will become a champ. It's on you whether you want to be a champ or not
1077.075 -> Next pattern
1082.586 -> Printing in the reverse order
1084.947 -> 1st Obs - row =n
1094.886 -> You can see that inner loop is running 'row' times
1117.481 -> Now what we have to print
1123.283 -> You can notice that numbers being printed are starting from 'row number'
1130.539 -> But we have to use the value variable if we want to print this pattern. What if we print it without using any extra variable
1138.456 -> Bhaiya, isn't it similar to the a question in which we printed numbers in reverse order. Yes you are right
1146.714 -> What if I do this? n-j+1. What will happen
1162.443 -> It is not giving correct output. Then how to correct this ?
1178.257 -> What if I print i-j+1
1185.751 -> YESS, it is giving correct output for every position
1237.593 -> So we got our formula = i-j+1
1241.31 -> I just tried Hit and trial method to get this formula
1246.072 -> Now let's code it
1247.302 -> After taking input, I initialized 'i' with 1
1253.419 -> and then I traversed over each row
1259.95 -> Inside this loop, we know that we have to go till the row number,
1280.221 -> Then we have to print i-j+1
1291.151 -> Then incremented j, added a new line outside the col while loop and then incremented 'i' too
1302.508 -> Let's run it for 3
1305.363 -> It gave correct outptu
1307.363 -> For n=4 also, it gave correct output
1311.613 -> Let's try a bigger number, n=15, again it gave us the correct output
1320.855 -> Let me add space
1327.876 -> First we observed something and then found a formula by Hit and Trial. This is how we solve pattern problems
1338.48 -> We have done 9 pattern questions. Let's do some more questions to understand this topic completely
1353.421 -> Next pattern looks like this
1371.448 -> 1st obs - row = n
1376.955 -> 2nd obs- A is mapped to 1, B is mapped to 2 and so on
1396.738 -> This time we will run both loops from 1 to n because it is a square pattern
1428.055 -> Now we have to figure out, what we have to print here
1438.452 -> We can't make a relation depending upon the column number because value is not consistent in a column for different rows
1478.121 -> But we can try out for rows. Let's try it out
1484.878 -> what if I say that you have to print ( char ch = 'A' + i -1 )
1495.419 -> Did you get this?
1497.907 -> Let's replace 'i' with row
1511.293 -> Let's check it for first row
1516.06 -> It is giving 'A'
1523.166 -> If i=2, it is giving us 'B'. So it means that it is working absolutely fine
1535.504 -> This is our relation = 'A' +i -1, where 'i' is the row number
1550.331 -> Let's run it
1554.847 -> Took input, First I am traversing on all rows and columns of this square pattern using the same method
1577.596 -> Then we have to print 'A' + i - 1
1587.776 -> Then incremented col, added a new line outside the col while loop and then incremented 'row' too
1602.05 -> Done. Let's run it for 5
1616.598 -> Bhaiya what is this. It is converting character to 'int' after after performing this calculation here but we don't want this
1629.804 -> So let's first store it in a char, so that i gives correct output
1634.968 -> Now it is working fine for various inputs like 5, 3
1662.317 -> Got it? Now let's move on to the next question
1671.319 -> No try this one
1676.935 -> I will call this as row or 'i'
1686.664 -> and will call upper one as col or 'j'
1692.707 -> You can see that the printed number is depending on the col.
1698.79 -> Now this is the relation which we will write= 'A' - j +1
1706.42 -> Done, simple
1708.478 -> Let's check whether we are getting correct values or not
1721.867 -> 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
1731.946 -> This is your homework
1735.964 -> Do this now
1746.294 -> Simple. Observation - Simply counting is there(in terms of character)
1752.914 -> I will take a start variable ='A' and then print it and increment it to 'B' and so on
1772.89 -> It is similar to that counting question
1778.534 -> There we stored a integer value, but here we will store a character. Done
1790.103 -> This is also your homework
1794.879 -> Let's do this one
1802.27 -> It is similar to this question which we did earlier. It is very easy
1812.509 -> Now what should be our relation, as it is depending on both 'i' and 'j'
1821.24 -> Let's write row and col number first
1835.217 -> What if I do i+j-1, let's try
1847.239 -> We will convert this 1 into 'A' later, but first let's check out the values
1883.429 -> 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
1892.591 -> What if I add 'A'-1 on both sides, let's try. See we got our formula
1940.843 -> Bhaiya how you calculated this, its very difficult. No it is not, see.
1944.057 -> We got our counting correct, but just had to map 1 to 'A', so to do so, we added 'A' -1 on both sides,
1973.32 -> Now the code is same, we just have to change the formula.
1980.473 -> First I am traversing on all rows and columns using the same method - Square pattern method
1994.329 -> then here we will simply print = 'A' + i + j - 2
2004.547 -> Then incremented col, added a new line outside the col while loop and then incremented 'row' too
2016.099 -> Simple
2018.449 -> You have to implement it by yourself
2029.887 -> Let's try this triangular one, but with characters this time
2036.208 -> 1st Obs - Printed value is depending on the row number
2047.328 -> 2nd obs - Number of characters in each row = row number
2059.061 -> So first we will run a loop from 1 to n for traversing to all rows
2073.131 -> And inside this loop, we will run the col loop till the row number
2085.617 -> Now let's figure out what we have to print. We can see that we have to map the row number with characters somehow
2103.138 -> Can I write = 'A' + row -1
2112.955 -> Trying this formula for different positions
2136.614 -> It is working fine, so this is our formula
2140.803 -> So we will print 'A' + row-1.
2142.719 -> Then incremented col, added a new line outside the col while loop and then incremented row too
2150.323 -> Let's code it and run
2152.403 -> Took input of 'n'
2156.538 -> So first we will run a loop from 1 to n for traversing to all rows
2165.462 -> And inside this loop, we will run the col loop till the row number
2184.15 -> 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
2225.444 -> Then incremented col, added a new line outside the col while loop and then incremented row too
2234.48 -> Let's run it for 5
2238.019 -> Oh, we have to write 'row' here inplace of 'i'
2245.56 -> See we got our output
2250.628 -> 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
2268.225 -> See this one
2279.401 -> It is simple counting. I will initialize a variable with 'A' and increment it after printing. Simple
2298.034 -> 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
2318.557 -> Let's increase the difficulty level
2325.349 -> Bhaiya, this one is easy
2334.496 -> 1st obs - row = 1 to n 2nd obs- Col = 1 to row because it is a triangular pattern
2364.168 -> It is depending on the row number. Let's find out the relation
2370.843 -> Can I do this, 'A' + row -1
2373.787 -> It is working fine for 1st element
2378.595 -> Let's check for this 'D'
2381.74 -> Oh it is giving wrong output, what to do now
2396.071 -> With this formula, we will get answer same for each row, so what changes we can make
2409.771 -> Should I include 'j' also in my formula? Oh yes, it is changing with 'j' too
2420.208 -> Should we try this one = 'A' + row + col -2
2430.212 -> It is similar to this question
2436.261 -> Let's try to get this 'D' by using our formula
2456.186 -> see we got 'D'
2458.928 -> Let's do for this 'G'
2461.62 -> We got 'G'
2481.598 -> So this is our formula
2484.502 -> 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
2496.494 -> I will show you the code
2499.497 -> I will just make few changes here in this code
2505.724 -> See it is giving correct output on running
2510.324 -> Let's move to the next question
2516.073 -> Now try this one
2524.929 -> We can see their dependence on row number and col number
2541.09 -> We know that it is written in the reverse order. Also 'A' will be there for sure
2557.234 -> Now what should we add in it to get 'D'
2561.127 -> Can we add J? No, then we won't get this C
2583.139 -> Let's find something else
2585.549 -> What if I do something like this, n-i-j+1
2595.114 -> We are getting answer for 'D'
2609.919 -> Let's try for this 'D'
2615.159 -> No we are getting wrong answer here
2637.138 -> Now what to do
2642.831 -> What if I just find the starting character and then keep on incrementing it till the end of the row
2657.265 -> What if I do something like this, to find the starting character
2665.525 -> For first row, we are getting correct answer
2671.929 -> Let's try out for 2nd row. Again we got correct answer
2678.085 -> So we came to know that the starting character of every row is 'A' + n - i
2688.857 -> Then we just have to increment it
2692.407 -> Okay, we got the logic. Now let's write the code for this one
2696.199 -> Took input
2704.684 -> 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
2727.369 -> Now let's make a starting character
2736.386 -> start = 'A' + n - row
2746.583 -> we will then print this starting character here
2750.71 -> After this we will increment it by 1
2769.468 -> Then incremented col, added a new line outside the col while loop and then incremented row too
2782.506 -> Oh sorry, I used i inplace of row, let me fix that
2796.411 -> See we got our output
2806.228 -> It was very easy question. We just have to find the starting character. After that it became very simple
2810.74 -> Can we simplify this formula which we made for this question which we did earlier
2831.08 -> This is your homework. Do this one
2850.686 -> Now let's try out this variant. It includes spaces too
2863.301 -> You can see that before printing these characters, we have to print some spaces too
2870.034 -> Inshort, we have to use two spaces, one for printing space and other one for character
2880.253 -> see, it is looking similar to square matrix
2886.636 -> You can see that numbe of spaces = n-i
2927.917 -> So one loop will run for space time, and it will just print space
2946.806 -> Now, after then we have to print a character. you can see that number of characters = row number
2956.305 -> So we will print star inside this another while loop
2970.156 -> Then we entered a new line, increment row and boom, we are done with our code
2982.778 -> Now let's code it and run
2985.069 -> First took our input, then we will traverse over all rows using this while loop
3010.086 -> First I will print spaces
3017.196 -> and then I will print stars
3022.632 -> For spaces we can do something like this
3041.095 -> Now let's print stars
3042.323 -> We will run a loop (row) times and keep on printing stars
3067.856 -> Then we will end the line and increment row
3075.322 -> Again, I used i inplace of row, let's fix it and run it
3084.774 -> see, it gave correct output for 4
3088.653 -> Now you can also solve these types of questions easily
3101.373 -> 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
3118.916 -> Here the number of characters in each row are equal to n-i+1
3141.892 -> See it is giving correct output
3156.081 -> So star = n-i+1. Very easy, right
3165.489 -> You have to write code for this. It is your homework. If you will complete homework questions, then only you will improve
3174.647 -> If the same question would have been given like this, then you need to print the spaces too
3186.767 -> We can see that spaces are increasing as the row number is increasing. So No. of space = row number-1
3205.429 -> Stars are same as above question. Stars = n-i+1
3217.287 -> This was also easy question
3221.206 -> Write code for this as your homework
3226.934 -> Can I make something like this
3236.144 -> This is also your homework
3239.647 -> How to solve this one?
3249.389 -> This is also your homework
3253.64 -> Now let's see this one
3264.473 -> This one is also your homework
3268.379 -> One another question. This is also your homework
3276.685 -> These are very easy questions if you have understood previous questions
3283.727 -> Now let's try a hard question
3301.552 -> We can see that there are three components - Spaces, a triangle and one another triangle
3323.28 -> If you are able to do this one without my help, then it is very good for you.
3351.513 -> 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
3358.681 -> 1st obs - Spaces = n-row number
3393.92 -> This inner triangle is similar to this question which we did earlier
3422.535 -> We are simply printing J, see how easy it is
3427.898 -> Now see this triangle
3431.59 -> But before that, let's print the other two things, then we will come to it
3439.827 -> First we took the input n
3445.201 -> We will start from 1 and go till 'n'
3452.371 -> We have to print 3 things - Space, 1st triangle, 2nd triangle
3467.353 -> For the first part we will do something like this
3475.985 -> Spaces = n-i
3480.285 -> and then run a loop space times
3484.288 -> We will simply print space and then decrement it
3490.513 -> Now to print the triangle, we simply have to print J
3495.746 -> We will run a loop from 1 to row(because it is triangular)
3509.883 -> Then we will print J and increment it
3517.988 -> Let's run for this one and see what will happen
3534.637 -> For 4, it gave us this output which is correct, but we have to print the 2nd triangular pattern
3543.44 -> Let me change these things here
3555.658 -> See, try to find it's relation with this row number. Got ?
3583.313 -> The first letter is starting from i-1
3594.972 -> and after that we are simply decrementing it
3600.421 -> see, start = i-1
3609.655 -> then we will Simply print the integers in reverse order
3624.459 -> Let's run it now
3636.017 -> See it gave correct output for 4
3638.759 -> There may be some other methods to solve these questions. We can do with anyone
3651.283 -> Now you are champ in pattern questions. Complete that homework questions too, then you will not face any problem in these types of questions
3661.726 -> Now do this hard question, if you are able to do it, then you have become a champ in patterns
3704.241 -> It is known as Dabangg pattern
3709.035 -> This one is for n=5. It is very easy if you will do it by diving it into different sections
3718.346 -> I will give you the hint
3722.757 -> I can do it by dividing it into these sections
3734.052 -> You may have some other approach. This one is your homework
3742.815 -> So we have covered almost everything, if else, nested ifelse, loops, conditionals,
3749.582 -> 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
3768.757 -> I myself have implement 14-15 questions and rest questions are given as a homework to you
3775.298 -> If you will not implement these by yourself or not do homework, then you will not get anything
3782.812 -> 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
3791.986 -> This was for today's video. In next one, we will see operators, bitwise operators,
3797.672 -> For loop, and many more interesting things
3804.818 -> Now we are comfortable with variables, data types, conditionals, loops, so we can start practicing questions too
3815.736 -> You should finish your homework. Also tell the answers in comment section for the questions which I asked inbetween of the video
3830.527 -> So this one was for today's video. Will see you in the next one. Till then, Bye

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