Solve Any Pattern Question with This Trick!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another video and this is an important one so before we get started i just want to give a huge shout out to my course which is helping thousands of people so please make sure you check out the links in the description this is this video is a part of the complete data structures algorithms interview preparation java boot camp pause this video right now and subscribe why why should you subscribe because i'm providing this course for free it is one of the best courses you can do right now for free and covers everything i have the entire detailed agenda in the description on which date i will be uploading which video and so far people have loved it our discord community has also gone like really why you know like it's twenty thousand twenty five thousand uh people are in the discord channel we only started it a month ago please subscribe please share with your friends join the learning in public initiative so when you do something like when you learn something share about it on public like on twitter and linkedin use hashtag bsa with kunal hashtag 60 days of dsa the twitter bot is going to retweet it so you can check out the twitter bot as well the 60 days of dsa one over there you can see so many people are benefiting from this course so don't miss out do it pause the video subscribe share with your friends like right now right now you have to like okay otherwise it will not work because you have to support me in providing such amazing courses and you don't even have to trust me just watch the you know entire comment section thousands and thousands of people are commenting check out the shout outs bought on twitter check out our shout outs on the discord channel or if you want to ignore all of these things just watch the videos yourself and you will fall in love but let's move forward with it uh in today's video we're learning about patterns so you can see some patterns over here like this is a star pattern this is some triangle some other triangle okay some number triangle or whatever some half rhombus or whatever right another triangle with some spaces over here and some r glass or whatever so by the way yeah we have assignments as well so assignments notes and discord support and everything is here so these links and everything are in the description below make sure you check it out i trust you that you might have subscribed till now if you are new to the channel it will really help me in me in reaching every single student in the world okay it when you subscribe and when you like it will help me in reaching so many people around the world who will benefit from this course because you will not find this on anywhere on youtube a course like this or such a complete course like this these are not my words these are the words of students let's get started so why are patterns important um patterns in like programming languages like using loops we have already learnt about loops so link in the description so patterns are important because it enhances your thought process in some companies like no one really asks patterns like this but you can use the problem solving skills that you have practiced via patents in your interviews rarely sometimes i have heard about experiences of where patterns are asked in coding interviews i have heard about experiences like my friend he was in a company he went to give a role of a company there they asked some patent questions but to be quite frank no one really asked this okay an interview directly no one will ask this but why is it important because if you are learning programming for the first time if you're learning a language or the programming you did not know about loops you don't know about variables you don't know about input output or whatever these patterns are very important to bring to build your foundation so if you are new in programming if you know in data structures algorithms if you know in algorithms and you know all these things please make sure you focus a little bit on patterns because it is only going to help you improve your thought process so now that we know why we are doing patterns let's look into how we can solve these so this video is special as by the title you can see it says solve any pattern easily with this trick you might be asking kunal what is the trick what is this trick that you are talking about kunal what is it tell us okay uh please make sure you like share and subscribe and now let's look into it so the trick on this is that uh i will write this down okay so all these patterns that you may see things may look different things may look different but all of these things have some things in common i will randomly choose all these patterns and create these and it will be easily solved there's a few steps you need to take when starting to solve these problems i will tell you these step by step so in this video obviously i'm not going to solve all the 31 questions but i will tell you the steps with you with which you can solve let's say a thousand patent questions i will tell you that step i will tell you that trick by which you can do this definitely has worked for me let's look into it so the prerequisites are very simple you know we have already covered uh loops so the prerequisites are loops so you should know about like loops and stuff i think if you are so far in the playlist you might already be knowing about it patterns questions tricks how to approach a problem why this trick you can solve any problem of patterns how to approach okay point number one run the outer for loop run the outer for loop the number of times you are having the lines okay for example if the pattern is this so in every pattern like this you will see rows and columns you will see rows and columns so outer loop because you know if i talk about matrices we already learnt about matrices so matrices also you have like rows and columns outer for loop is for rows inner for loop is for columns very simple stuff right if you're not aware of it make sure you check out the arrays video the link is in the description don't comment things like hey kunal we don't know about double for loop kunal we don't know about it why are you asking such questions when i have already covered it in the playlist so go to the playlist check it out 2d arrays i've also added timestamps then come back to this video we have covered it in detail how to traverse 2d array nested for loop still i'll give an introduction over here a little bit of hint so as we know these are the rows these are the columns here in this entire pattern we have four rows and in first row i have one column in second row i have two columns in third row i have three columns and in fourth row i have four columns as you can see over here very simple stuff in the first row i have one column two columns three columns and four columns okay so this outer for loop the number of lines is actually equal to number of rows so i know that number of lines is equal to number of rows is equal to number of times outer loop will run does that make sense number of times the outer loop will run let's try to see let's try to do it till now so if i go back if i go back over here and i say let's say pattern number one okay i say static void pattern one and let's say i just pass an integer n over here so let's say for n is equal to four let's say i wanted to do the same pattern that i just wrote on a white board so let's say n is equal to four what is the step number one to approach a problem what is the step number one to approach a problem look at the pattern see how many lines you have in the pattern that many times the outer for loop will run no problem that's it this is the number of times my lines will run that's step number one okay no problem let's see step number two what is step number two for every pattern this is not just for this one this is for every single pattern step number two is what is step number two step number 2 basically means how many columns are there with each row so basically identify for every row number how many columns are there or types of elements in the column for example here we have star but it might be possible that you have something like 1 2 3 4 5 6 7 8 9 10 so you have to identify how many columns are there so i can say that if this is row number if i'm writing the no row number over here row number this is let's say row number one row number two row number three and row number four in row number one how many columns are there one in row number two how many columns are there two in row number three how many columns are there three and in row number four how many columns are there four so in this particular example number of columns for every row is equal to that row number or not i figured it out point number two point number two is for every row number identify how many columns are there and what type of columns are there so in this case uh star type of columns are there no problem we can do it we can do it we can apply row number two so we can apply this rule number two so rule number if i'm just writing it from let's say 1 and less than equal to n no problem that is also fine so for i is equal to 1 or if i just write it as row for row is equal to one now for every row run the column for row is equal to one how many columns will be there one for every row row how many columns will be there r o w so for column starts from 1 column is less than equal to row that's it what do i need to print step number three this is like it's included in step number two only like what do you need to print okay all right i'll just mention it as a separate step okay never mind because in more complex patterns it will make much more sense so step number three is what do you need to print what do you need to print this is step number three that is it this is all you need to solve all the pattern questions that's it here i just need to print star for every element so what do we need to print for every element star no problem we'll print star nothing major in that i can say star like this i can add a space over here if you want no problem so what will happen is it will save row is equal to 1 okay then it will run okay for row is equal to 1 column is equal to 1 column should be less than equal to row means row is equal to 1 so column should be less than equal to 1 is 1 less than equal to 1 yes it is print star then column will be equal to 2 is 2 less than equal to 1 no it's not come out of this loop so when one line is printed when one row is printed we need to add a new line one row is printed new line second row printed new line third row printed new line same thing we were doing in 2d arrays questions that we did previously when you are printing a 2d array that is what we were doing print a new line let's try to run this pattern 1 let's say for n is equal to 4 three step process very easy stuff so simple if you want to debug it you can currently row is equal to one column is equal to row okay one so for for row number one it's going to run this interloop interloop of column one time only because it's running the loop row times rho is equal to 1 initially so it will run it 1 times column is equal to 1 is 1 less than equal to 1 yes it is print star and column plus 1 column is now equal to 2 is 2 less than equal to 1 no it's not that's why it came outside when it came outside this for loop it print new line now row will be equal to 2 column again starts from 1 is 1 less than equal to 2 yes it is ok pin star now column is equal to 2 is 2 less than equal to 2 yes it is print star 2 stars have been printed 2 stars have been printed now it will be like row is column is equal to 3 is 3 less than equal to 2 no it's not it will come outside of this loop and print a new line check it this out here it will print a new line new line now row will be equal to 3 it will be like hey is a column 1 less than equal to three yes it is print star you can see over here it's printing star print another star print another star new line print another star pinata star another star another star new line now row will be equal to 5 is 5 less than equal to n no it's not outer for loop will also end program will end that's it very simple stuff that's it with these three approaches you can solve any problem let's try to solve more actually this is pattern number two of the assignment so i'm going to name this as pattern number two name this as pattern number two no problem let's try to solve another problem let's say pattern number one this is very simple step number one is what identify the number of rows so number of rows is equal to n itself identify the number of columns in every row the number of columns are also n itself and step number three what are we trying to print we are printing star no problem no problem copy this paste this pattern number one for every row is running n times every column is also running n times that's it that is it that's it done let's try to solve some other question let's try to solve this question pattern number three pattern number three very very easy question why is it easy because we are following these three steps that's why it's easy pause this video try it yourself like share comment right now then try it it's very important because otherwise this course will not reach around the world i want people to know about it and not just this video is a very simple video when we do dynamic programming graphs you know recursion and space and time complexity object-oriented programming development machine learning devops all these things for free please comment like subscribe and share right now okay pause this video try it out okay hope you okay let's see you can come after the clap okay hopefully you tried it out let's try to solve it with this approach only with this approach only pattern number three so approach number one how many times the rows are there how many rows are there if this is n is equal to 5 then 5 rows are there so outer loop will not change okay how many columns are there for every row for row is equal to 1 columns are 5. for row is equal to 2 columns are 4 for row is equal to 3 columns are 3 for row is equal to 4 columns are 2. for row is equal to 5 column is 1. so can i not do something like this or every column how many columns column is actually going to start from one till number of times so for example yeah that's going to the same thing it's going to start from like 1 like but how many less than equal to is it going to be it's not going to be equal to i it's basically going to be let's say if here i is equal to 1 and i need 5 okay so basically this is 5 minus i plus 1 that's the thing n minus i plus 1 n minus i plus 1 let's try to run this i will explain to you how this happened no problem n minus row plus 1 sorry run simple stuff why think about it so let's try to you know discuss it let me bring up my whiteboard okay so try to just write it down and basically one more hint i can give you is in order to solve the problem is try to write down the row numbers so for example if i am writing one two three four row number one two three four in row number one i have let's say for n is equal to four this is the output in row number one i have four columns three columns two column and one column so initial first of all i need to find you need to find like a formula so this is another you know important point try to find the formula i will try to write it over here try to find the formula relating rows and columns so here for example if row number r for row number r is going to be like column is equal to equal to what here n is equal to 4 so this is actually equal to n plus 1 minus r so basically n plus 1 minus r this is just from common sense n plus one minus r because one more thing by which you can initialize you can see how can how did you know it was n plus one minus r was first this was n n minus one n minus 2 n minus 3. so this minus 1 minus 2 minus 3 is what minus 1 minus 2 minus 3 is what r okay for example if you had loop running from let's say because for example example in this case we are writing loop running from rho is equal to 1 rho is less than equal to n you can also do rho is equal to 0 rho is less than n there's also something you can do for example we can do something like this row number 0 1 2 3 now the formula will change to n minus r here it was n knows 3 2 1 so n minus 0 n minus 1 n minus 2 n minus 3 what is this minus 1 minus 2 minus 3 minus 1 is equal to what r minus 0 r minus 2 r minus 3 3 is also r all right that makes sense cool let's move on to the next question now let's look into the next question which is this one so here it says that okay um what is our first step first step says how many number of rows you have no problem coil paste pattern i think this is fourth yeah so it has let's say if this is for n is equal to five then five rows so outer loop will run n times outer root is running n times okay column is actually running uh for every row how many columns do we have eq the the number of columns for this is roll number one that we have one column row number two we have two column row number three we have three column so relationship between row and column is equal to what r is equal to c so this should actually be equal to less than equal to rho how many number of rows you have that many number of how what is the row number that should be equal to the number of columns in that row this row number four it has four columns but one more difference is that we are not printing star right now what are we printing you tell me we are actually printing the the column number column number one column number one column number two column number one column number two column number three column number one column number two column number three column number four so we are actually printing the column number itself let's try to run this pattern four very simple stuff same thing you can add a space if you want to make it move look to make it look more neat okay very simple pattern number five this is a little bit different but it's uh very simple again step number one don't be no don't be don't be like uh what do you call it don't be afraid of this thing follow the steps what is the steps step number one how many um how many number of rows do we have how many times the rows in the outer for loop will run outer for loop will run the number of rows time for example this was 4n is equal to 5 then how many total number of rows do we have 2 n minus 1 2 n minus 1 because here we have 9 rows that's fine pattern number five no problem static void pattern five in ten outer loop will run how many times step number one figure out how many times the outer loop will run so it will run n minus 1 times 2 sorry 2 n minus 1 times less than 2 into n that's it that is it that's it now you need to figure out how many columns do we have let's say we have c number of columns at every row this is the next step right figure out how many columns you have at each row initially let's say it's whatever okay it does not matter i'm just putting some dummy value over here okay so here i'm just going to say okay let's say this is the number of columns we have then for i is equal to then print that many amount of stars okay no problem print that many amount of stars no problem no problem then print a new line over here no problem i'm gonna rename this to row that's it only thing remaining now is actually figuring out how many columns do we have at every step how many columns we have at every set you can see we have already done a problem like this question number two the problem is that this is a different part and the number of columns is changing accordingly so you can see that first the number of columns are increasing till n is equal to 5 then after so for example till i till row is equal to n you have to add normal columns row is equal to one one column row is equal to two two column row is equal to three three column no problem so i can say that um i can say if i is actually less than equal to n sorry uh row is actually less than um or one second let me just try to you know think about it so here i'm actually saying that it starts decreasing it starts decreasing after the value of n so the value of rho starts decreasing after the value of 10 let's try to let's try to write it on a white board and bring the formula everything we have done is same only thing remaining now is the formula that we talked about relationship between c and r that is something we need here right now it contains dummy value we actually need a formula let's see how we can get the formula i can also rename this if you want total i can rename this to something simpler total columns in [Music] row okay we need a formula for that okay let's see how we can get it so the question was something like this it says the pattern is like um what is the pattern let's say for n is equal to five pattern is something like this one two three four five and after this it will start decreasing okay let's write the row numbers over here so let's say this is row number zero one two three four five six seven and eight outer loop is running n minus one times nine times that is correct that's why we're doing uh zero till uh no less than 2n that's fine so here number of columns are equal to what columns here is equal to what column here is equal to 1 2 3 4 5 4 3 2 1 these are the columns these are the row number we need a relationship between this we need a relationship between this no problem so i can say column at every row is going to be equal to what we need a relationship between this so we know that if the row number is actually greater than n in that case it will be something different otherwise the column number is just going to be equal to row only but when it exceeds when it exceeds this in that case what will happen so for example if i take this one here the row number is 5 for example and uh or let's say i take this one here the row number is six for example um so i need column number three here row number is seven i need column number two so i need column total columns here 2. so it's actually subtracting from my total row number it's going to be equal to columns are equal to what how many columns do we have over here try to figure out brainstorm think about it pause the video column is equal to okay total row number that you have or in one second total row number that we have no no no how many how many do we need to delete we can see that first so how many do we need to delete from here if let's say n was equal to 5 and i'm talking about this thing so it should be containing like let's say some 7 or something so sorry a total it should be containing at maximum n only it can only contain like at maximum five only we have to remove these three so this three how do we get this three how do we get this three this three can actually be equal to equal to what row minus n this can be equal to rho minus n okay row minus n minus 1 something like that all right so in this case let's say we are ignoring um three so i can say this is like uh these are the ones that i need to ignore this is going to be row minus n row minus n minus 1 no problem row minus n minus 1 so 5 minus sorry row minus n plus 1 minus n plus 1 so 5 sorry 7 minus 5 which is 2 plus 1 3 this is what i need to remove this is what i need to remove from what do i need to remove this from what do i need to remove this my entire n my entire n so this becomes what 2n minus row minus 1 let's try to write this down just try to code this thing just try to put it in code so here actually this is equal to columns is equal to what if i can say if my row is actually greater than n n in that case it's taking what column is equal to n minus sorry 2n 2 into n minus rho minus 1. otherwise it's equal to row only let's try to print this very simple stuff but uh okay here we are actually one second it should not be minus one i think because total ah okay okay okay so this was the total number of columns but we were doing less than over here so either you can do less than equal to or i can do i can remove minus 1 from here because the columns one is also running from 0 right so i can remove minus 1 from here this should work tada very simple you can try it for n is equal to 5 if you want works well pretty cool stuff right so yeah as you can see very simple only thing is the only formula we are following the steps we are following okay find the number of rows find the relationship between row and column in each column print that many amount of time and print what you want to print let's look into another question let's say one more thing that can happen in questions is they will ask for some spaces so we'll look into these spaces thing as well but let's try to pick up a difficult question not difficult but like more advanced so this is something you can do triangle something you can do something you can do already this is something you can do already is also fine let's try to solve a question that covers all of these things okay let's try to solve a question that covers all of these things which one we can do let's try to do this one because here our spaces are also covered and everything is covered okay let's try to do this one so how many times will the outer loop will be running if n is equal to five outer loop is running two n times i'm just going to copy this this is pattern number 28 two end times the loop is running what is the number of columns or number of stars in every particular one same same as previous because yeah exactly the same as previous that's correct pattern number five one two three four and four three two one that's also correct this is also fine are we printing star yes we are printing star only but only thing remaining is before every row you have to print some spaces also so let's say you have to print some spaces let's say in spaces number of spaces okay number of spaces is equal to something print that many amount of spaces spaces will print that many amount of spaces that's it but how do we figure out the number of spaces what are the number of spaces over here so here if n is equal to let's say you know um what are the number of spaces here one two three and four so four spaces three spaces two spaces one space zero space four three two one so here column is one spaces is four column is two spaces is three column is three spaces is two so spaces is actually equal to total number of n minus columns because here total number of columns are 4 and number of spaces are 0. so spaces can be equal to n minus c right total number of minus column so here example columns are 5 n is 5 number of spaces is equal to what 5 minus 5 0 that is correct here columns are 4 number of spaces equal to what n minus 4 which is 5 minus 4 1 that is correct 5 minus 3 2 2 space is correct 5 minus 2 3 3 space is correct 5 minus 1 4 spaces correct that is also correct over here because we have already figured out the number of columns in every row we don't have to worry about this case here it will be the similar n minus four one one space n minus three uh two two spaces okay no problem spaces is actually equal to what n minus total columns in that row run this i actually did not run it no i did not call the function pattern number 28 run it looks good diamond now you can make you know above once as well you can make this one you can make this one this one this one you make this rhombus space one whatever you can make this one this one this one if you want this one if you want i can try you know help you out in this one no problem this is you can make you can make this you can make this you can make this this is you can make you can make this one you can make this one this one also you can make so many patterns you can make on your own does not really make sense for me to you know do it again and again okay let's try to see some other pattern if you want some different pattern let's see this one this is a different pattern so let's say we do this one the outer loop is whenever it's given numbers try not to use zero index because then you can directly use the numbers okay so i'll try to solve this one also static wide this is pattern number 30 okay pattern 30 in 10 no problem step number one how many times the entire loop is running if n is equal to 5 it's running 5 times so row starts from 1 less than equal to n no problem what is the next step next step is equal to how many times the the loop is running columns so here you can see column is first running from i till 1 and then it's running from 2 till i so basically if i am saying row this is row is equal to 4 so it's running from 4 till 1 then 2 till row from row number till 1 then from 2 till row number no problem from row number till 1 column is equal to from row number till 1 column minus minus so like 4 3 2 1 3 2 1 or whatever it's going to print the column number and after that it's running from two till less than equal to row number 2 till less than equal to 4 less than equal to 5 okay and here column plus 1 that's it only spaces are remaining how many number of spaces will be have over here this is as similar as the previous one that we did this one spaces in this case were equal to total n minus through same logic that we used over here okay so here we have column is equal to one so the spaces are n minus 1 which is 4 spaces column is equal to you know uh sorry row number is equal to 2 so your row number is equal to 1 total spaces are n minus 1 row number is equal to 2 total spaces are n minus 2 so i can say int or i can just run spaces loop directly also or space is equal to 0 space is less than you know n minus row so i'll just print a space for this i can say print a space for this and run it how difficult was it very simple i did not add a new line no problem need to add a new line whenever all the loops are run add a new line let's try to see if we are able to make it like this very nice let's add a let's add a space also so it looks prettier and i can add an extra space over here because i'm adding spaces over here so i need to add one space over above as well very nice looks much prettier very cool similar question you'll find over here in 17 i think you can do this like on your own yeah so i'm just going to say copy paste because everything is same question number 17 so basically the idea is now it's actually running uh over here it's basically going to run till so in 17 it's running from the number of columns the number of column c the number for that column for that column for example one two three four three two 1 that is what you want to do if i say in c is equal to something so it's going to start from that column number and it's going to go till that column number only that's the only change now we have to figure out how do we figure out this c number okay and since we are following our rules then this entire like outer loop will run how many times because we did copy paste but we have to fix a few things over here outlook will run two end times so 2n minus 1 times that's why we have it over here like uh you know we have uh 2n over here so it's going to run 2 n minus 1 times that's why we have it from 1. so it's going to be like 1 2 3 4 five six seven which is equal to yeah that's correct cool and uh it might have like you know one space two space above it or whatever you can if you can fix that if if such things come into any any pattern question or whatever um c is actually equal to what number of like number the column not actually the number of columns but this particular one it's one two three four three two one so this is similar to what we did previously this one this one i explained this already so this is actually in c is equal to like this and this is also going to be equal to um space is equal to n minus c n minus column right so very simple stuff right i think that looks good let's try to run this pattern number 17 that uh very simple like share subscribe comment right now pause the video and do it all right please cool let's look at one more question interesting now you can solve every question okay don't worry ah it's very simple this is also very simple we did it this is an interesting question so let's do it pause this video try to do it because this is sort of like a different question it's very fun also so try to solve it and then come back to this question and then i will show you the actual answer which was very very easy you will be like oh we never thought about this this way sometimes you have to actually look for patterns in patterns okay let's do it now before moving forward i also want to share like sometimes you know one or two extra lines or spaces if that is coming in between your more readable code and let's say you know obviously they're not going to ask this in the interview so in that case that is why i'm making exceptions over here for example in pattern number five right i take pattern number five here the number of uh lines are let's say 2 n minus 1 but that's as you can see the loop is actually running two end times okay so even though i can do it something like this okay and then i can start by you know changing something in this but that would look very messy so if there's like one two extra line over above and that's totally fine because that's totally cool no problem in that as long as you are having a good readable code in these pattern questions specifically specifically you say that hey no extra lines are allowed or whatever then you can do it via your own okay cool so let's do this question um for example we have in this question we have if we follow our approach only so we can say something like you know total number of lines are uh if here n is four then total number of lines are 2 and minus 1. so i can say static void pattern 31 in 10 2n minus 1 so we are going to do this like uh the proper way no x lines or whatever how many columns do we have call number of columns is also 2 n minus 1 ok no problem or number of columns okay that's how many times we need our loop to run now at every particular index what value do we need that you need to figure out int at every index is equal to let's say something and you just need to print that what value do we need at every index then you just need to put there so what is the formula going to be for row and column that is what you need to figure out can you see the pattern over here everything follows the same approach only thing you need to put your brain into is this line what is the relationship between row column and n that's what you need to put over here rest everything is what we have discussed pause this video think about it then we'll come back so in this example let's try to think it on a different way so for example okay this is so fine so far so good okay true let's say here we are having this many amount of rows this many amount of rows that's okay at every index is what we need to figure out so let's look it into a different perspective okay let's try this out look at it at this perspective okay imagine like uh in order to make some things simpler here we said that for this is for n is equal to 4 right this is for n is equal to 4. for n is equal to 4 it's actually running indices of rows and columns from 0 to 8 till 2 of n it's running its indices so i can make let's say in order to make things simpler i can say n is equal to 2 into n just to make things simpler okay so if it's 4 then you know like this from 0 till 2 n it will be running so let's try to do that just to make things simpler so here i'm just going to say n is equal to 2 into n and it's just going to run till equal to 10 so now it's actually going to run from 0 to 8 that is exactly what we can see from the figure also okay very simple stuff now you might have done a lot of things from this pattern you can see at every index what is it saying what is going to come at this index if i talk about this index what is going to come at this index if you try to look at it carefully you can see that if i imagine a wall over here at the four corners i imagine a wall just draw a wall over here okay so if i imagine a wall at these four corners what can you see you can see that the value this is a very important point and this only comes with observation okay you can see that the value at every particular cell is actually equal to the minimum distance from all the four walls take any cell if i take this cell if i take this cell so what is the value of this what is the distance of this cell from left wall from left wall the distance is 1 2 3 4 five from top wall the distance is one two three four five six from right wall the distance is one two three from bottle bottom wall the distance is one and two what is the minimum of all of these things 2 that is our answer over here very cool stuff try anything else let's try for this one what will come over here so minimum of distance from the left wall is one from the above wall is one from the right wall is one two three four five six seven from the below wall is one two three four five six seven also minimum is one so answer will be 1 over here similarly for 3 and 4 whatever now is it isn't it simpler now or the previous question so what are these four formulas that we are getting over here if i'm talking about from the left from the right from up and from down so at any cell you are at what is the distance from the left of the wall for that cell for the left of the wall the distance is i will just write it down over here let's say um is actually not left right up down i'll write it clearly so distances from the left from the right from up and from down you tell me you tell me what is it so if i'm at um if i'm at row number six mean i'm already down six times so if i'm already down six times down is going to be equal to the number of row check for any case if i'm talking about this one so row number is five means there are five rows above it one two three four five that's correct so how many so what is the distance from the above wall distance from the above wall is equal to the number of row okay what is the distance from the left wall that is equal to the number of columns so if you are at column number 3 so there are three over here yeah from the left wall it's column what is the distance from the right this on the right is going to be equal to my n means eight minus total number of columns you have already had okay so if i'm talking about this thing talking about this thing so distance from right is three eight minus no one second um what is this going to be equal to i think it's going to be equal to n minus distance from the right wall which column you are at yeah which column you are at so i'm already ahead like these are like five columns ahead okay so this is going to be equal to this entire oops sorry sorry sorry into 4 n is equal to 2 into n 0 1 2 3 4 5 6 7. so basically what i'm trying to say is i need to figure out this value 3 this is actually equal to entire 8 minus how many far you have already come this is nothing but columns okay this is nothing but columns so actually 8 minus 5 3 that's true 3 so n minus columns similarly this is equal to n minus rows okay so if you're at 3 over here this should give me 3. so total number of n is 8 minus how many rows you have already come i have come five rows should you give me three one two three that's correct yeah minimum of all of these is the answer minimum of all of these is the answer let's try to code this thing okay so let's try to code this thing now i'm going to say that math dot minimum of math dot you can use this math.minimum function sort of a thing it's going to call minimum for four values okay so two values i can just write over here row and column then i can write n minus row and n minus column that is it let's try to print this how simple was that print for n is equal to 4 let me add a space as well okay let's add a nice space so it looks pretty looks pretty good the question is how can we make this pattern from this just do the opposite of it here if you are printing math dot minimum value from the wall so if it's saying me you know if it's showing me 0 and 1 and whatever this is i can just say that this is equal to original n minus 1 okay so i can say in original n is equal to n so i can say original n minus the minimum one that's it so instead of zero it will be four minus zero which is four instead of one it will be four minus one which is three instead of you know uh what what did we have over here over here we had three so instead of three it will be original uh sorry here we had uh what did we have here we had two i think four three yep so let's try it out um if i run this okay so here you're getting like an extra zero but i i need to do it i i had to do it like that way only because that is the only way i could explain you know the distance from the wall but now you can definitely modify it to you know sort of like ignore the zero and have the number of rows as it has in this no problem so that's like a homework for you now that you know the you know main concept and theorem try to debug it try to you know see how it's working internally and everything but that's pretty much about it all right so you just need to change the number of rows and columns for it and change the formula a little bit maybe all right that's it so isn't it cool like we're able to solve these questions like this so please make sure you follow these concepts you know the ones that i mentioned number of rows number of columns and what goes in every column the formula that's it that's it all right well thanks a lot for watching if you like this video i want you to do a few things okay like i mentioned throughout this entire video i know it might get a little a little annoying but it's important so that many people can learn the truth about the courses right so like right now comment right now like pause and comment then after you have commented you know if you haven't subscribed already subscribe that's very important and next thing is in the description you can find dsa with kunal hashtag and whatever so 60 days of dsa or dsa with kunal if you write a tweet about it the bot is automatically going to retweet it's called learning in public so make sure you check out the assignments as well assignment link in the description below so you can do the assignments please do these things please share please share the good work that you're doing and please share if you're learning something new because that will create awareness okay so this great free course will reach as many people as it can and all your honest reviews will go over there as well please share your honest views and uh please do all these things that i've just mentioned it will really support the work that i do i don't need any money from the students or whatever all the courses will be free this is the way i want you to support me okay so please make sure you do it right now once you have watched the video and uh yeah share about it with your friends and we will see you in the next one bye also check out the links in the description you can follow us on socials that is also going to be a big support [Music]
Info
Channel: Kunal Kushwaha
Views: 54,972
Rating: 4.9087977 out of 5
Keywords: java course, complete java course, java course free, Data Structures & Algorithms resources, free CP course, interview preparation course, pattern questions in java, pattern questions in coding, pattern questions in aptitude, pattern questions apna college, pattern questions in interview, solve pattern questions, how to solve pattern questions in coding, pattern questions in reasoning, pattern printing, pattern printing programs, patterns in c++
Id: lsOOs5J8ycw
Channel Id: undefined
Length: 57min 19sec (3439 seconds)
Published: Sat Sep 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.