Hello guys welcome to my youtube channel. Today we are going to solve staircase HackerRank problem. So in this Problem we have to print a staircase like this. If n=4
so the input we will be receiving an integer n, denoting the size of the
staircases. ln output format we have to print like this. If n=4 we
have to print like this, n=6 we have to print like this. let’s first
understand this how it’s Basically this is the input, you will be getting 6 and you
have to print like this so there’s 4 Suppose 6 is not here I am putting somewhere here 6. So here we have
how many rows ? 6 rows, you can see 1 ,2 ,3, 4, 5, 6. we have 6 rows and we have 6 columns also. let me show you See this is the first column , this is second
column, this is the third column and so on.. We have a fourth, fifth ,sixth column.
So we have to print a matrix basically of 6×6. This way, this matrix.
So here, if nothing is there is actually space So if it is a 6 , So we are printing 6-row number(6-1)=5
spaces then followed by a single # for a second row, we are
printing 6-2 spaces and followed by two ## for three we are printing 6-3 symbols followed by # symbols
sorry, first spaces and then hash symbol, for fourth row, we are printing 6-4
2 spaces followed by 4 # symbols. See, how many rows out there that
must symbol we have to print n – that symbol, that will be that space count I hope guys, Its clear to you.
so same we have to apply in our code. Definitely we are printing rows and columns then
nested for loops. let’s start coding So this is the function. here you see n is the input which tell you the number of staircases.and we have to print pattern like this up-to n. So definite, We have to go for two for oops, one for rows & one for columns. So this is the one loop which will take care of the rows,
how many rows we have to print?
We have to print the rows from 1 to n. Inside this we will be having one more for loop. why we are using n-i, will tell you So here you can see input which is 6 and row is first then we are printing 6 -1=5
spaces and for second n=6 We have second row, so we are printing 6-2=4 spaces. So in general case you have n as an input
instead of 6 – every row its we are decrementing. So it’s value 1,we are printing 5 spaces and if i value is 2, we are printing 4 spaces. So here we are printing the space and one more thing we should not use println because it will print the space in next line. So we’ll remove it. So after printing the
spaces, we have to print the # symbol. So we will take one for loop Which will print the # symbol If i=1 it will run from 1 to 1.
If i=2 then it will print from 1 to 2 means If it is the first row what you print first # only one time # and in the second row, it will print two ## symbol & in third row, it will print three ### symbols Here also we have to use the print because we have to print the # symbol in same line. and once you have printed one line you have to go in next line. because once you print complete line you have to go to the next line. Let’s run this program. So here you can see testcase got passed let’s submit our solution Yeah so this is accepted by hackerrank.
So guys, I have one more way to solve this. let me show you how? take one string str. Only one loop can solve this problem. you’ll feel amazing how this problem can be solved by using one loop but it’s true. just a sec guys, I will explain what i am doing First let’s run this and i hope it should work it’s time to submit this code. yep. So guys, let me tell you that idea behind this.
I have taken a temporary string which is a hash because every
line we have to print # and we have to print the hash to number that is given
for n .So we have taken value from 0 to less than n which is n-1 and here i am utilizing this
printf function which is useful for formatting and you can see in this
program we have to format our output basically. in a proper form so I am utilizing this printf function to format my output. so what this does. let me tell you. this is for printing and here every line it has to
print this much character and it’s 6 then it has to print 6 character basically and ‘s’ stands for string because we are printing string over here so the string length
will be 6 and ‘ n’ for new line so every time if your string length is less than
6 it will automatically append space before that so what is happening over
here and in starting String str only one hash is there but this is 6
so it has to print 6 length string which which length is 6 but here the length is 1 so will automatically append 5 spaces
before that and print. for the next line then string got updated with ‘##’
so in this time we have string length of 2 and it has to print length
of string 6 so what it will do? it will append 4 spaces before that and print
the string. so like that it will print the whole string.
I hope guys ,it make sense this is just like another way, how you
can do it. that’s all guys from my side and thanks for watching.