
Ninja wanted to go to a party along with his friends. However, his mom wanted him to go only if he completes a task assigned by her.
She gave Ninja a value and asked him to print an X-shaped pattern.
Example : Pattern for N = 3 (No. of rows = 5, No. of columns = 5) :
1 1
2 2
3
2 2
1 1
Since Ninja is in a hurry and doesn’t want to be late for the party; he asks you to solve the problem. Can you help solve this problem?
The first line of input contains an integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’.
Output Format:
For each test case, print every row in a new line (row elements not separated by space)
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 1000
Time Limit: 1 sec
2
3
2
1 1
2 2
3
2 2
1 1
1 1
2
1 1
In the first test case, ‘N’ is 3, so print the rows from 1 to ‘ 2*N -1 ’, and if the conditions are followed correctly, the above pattern is printed for N=3.
In the second test case, the value of ‘N’ is 2, so print the rows from 1 to ‘2*N - 1’, and if the conditions are followed correctly, the above pattern is printed for N=2.
2
4
5
1 1
2 2
3 3
4
3 3
2 2
1 1
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
Check if you can manipulate the nested loops to print the result?
The idea is to use nested loops in such a way that yields the answer.
The steps are as follows:
O(N^2), where N is the given number.
We are traversing each row of the matrix (2*N-1) time that takes O(N) time complexity, and inside each loop, we are using an additional loop between the columns for (2*N-1) time, which takes extra time (N). Hence the overall time complexity is O(N^2).
O(1), no extra space required.
As we are not using any extra space except for an array to maintain 26 characters. Hence, the overall space complexity is O(1).