Ninja is given a pattern. Now he is asked to print the same pattern for any given ‘N’ number of rows.
Note:
There is only one space between the values of each column in a row.
For example, Pattern for ‘N’ = 5 will be.
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
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.
The 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 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
For test case 1:
We print the given pattern for the given 5 rows where each row has different values in increasing order with a difference in the value of 1 between each element and 1 space between each column in a row.
1
4
1 2 3 4
9 10 11 12
13 14 15 16
5 6 7 8
For test case 1:
We print the given pattern for the given 4 rows where each row has different values in increasing order with a difference of 1 in the value of 1 between each element and 1 space between each column in a row.
Try to make the relationship between the row numbers and the initial value in each row.
We can observe that there are ‘N’ numbers of rows (where ‘N’ is the total number of rows to be printed). Each row contains a different value in the first column and its value increments by 1 for each subsequent column.
O(N ^ 2), where ‘N’ is the given number of rows.
As we will be traversing the given ‘N’ number of rows and for each row, we will be traversing it for ‘N’ no of columns so our 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.