Problem of the day
Ninja is given a pattern. Now he is asked to print the same pattern for any given ‘N’ number of rows.
For example, Pattern for ‘N’ = 4 will be.
1
232
34545
4567654
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains a single integer ‘N’ denoting the number of rows for which pattern needs to be printed.
Output Format:
The output of each test case will be 'N' strings denoting the pattern printed for the given ‘N’ number of rows.
Output of each test case will be printed on a separate line.
Constraints:
1 <=T <= 5
1 <= N <= 100
Where ‘T’ is the total number of test cases and ‘N’ is the given number of rows for which pattern needs to be printed.
Time limit: 1 sec
1
5
1
232
34543
4567654
567898765
For test case 1:
We print the given pattern for the given 5 rows where each row prints spaces initially for each row decrementing for each consecutive row and printing the values corresponding to each row according to the given pattern.
1
4
1
232
34545
4567654
For test case 1:
We print the given pattern for the given 4 rows where each row prints spaces initially for each row decrementing for each consecutive row and printing the values corresponding to each row according to the given pattern.
Try to make the relationship between the number of columns and spaces in a particular row.
We can observe that there is an ‘N’ number of rows where ‘N’ is the total number of rows to be printed. And each row exactly contains ‘N’ - ‘row’ number of spaces where ‘row’ denotes the row number starting from topmost as 1. Also, when we go left to right the value of each element in a particular row increases by one starting from the ‘row’ number and then decrements in the same column from 2* ‘row’ - 1 till 'row'.
O(N ^ 2), where ‘N’ is the given number of rows.
As we will be traversing the given number of rows and for each row number, we will be traversing it from 1 to that row number for printing spaces, and traversing 1 to row number, and 2* row -1 to row for printing the columns of the pattern, Hence the overall time complexity will be O(N ^ 2).
O(N ^ 2), where ‘N’ is the given number of rows.
As we are using an array of strings that takes O(N ^ 2) space.