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-
1234
123
12
1
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 for each test case will be printed on a separate line.
Constraints:
1 <=T <= 5
1 <= N <= 500
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
12345
1234
123
12
1
For test case 1:
We print the given pattern for the given 5 rows where the first row prints from column number 1 to 5 and no of columns reduces by 1 for each consecutive row.
1
3
123
12
1
For test case 1:
We print the given pattern for the given 3 rows where the first row prints from column number 1 to 3 and no of columns reduces by 1 for each consecutive row.
Try to make relation between number of columns in particular row.
We can observe that there is an ‘N’ number of rows (where ‘N’ is the total number of rows to be printed). Each row exactly contains the ‘row’ number of columns (where ’row’ is the current row number). And for each row in each column ‘col’ gets printed (where ’col’ is the current column number).
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 the columns of the pattern our overall time complexity is O(N ^ 2).
O(N ^ 2) where ‘N’ is the given number of rows.
As we are using an array of strings to store the pattern.