Students at Ninja School were bored with their daily routine and wanted to try something new. So, they thought about observing patterns. While observing the patterns, they found a very interesting pattern which they wished to explore. Your task is to help the students to visualize the pattern for any given ‘N’.
Given an integer ‘N’ you need to return the corresponding pattern for it.
For example, pattern for N = 15 will be : 1
123
12345
1234567
123456789
12345678912
1234567891234
123456789123456
1234567891234
12345678912
123456789
1234567
12345
123
1
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first and the only line of each test case contains a single integer 'N' denoting the number of rows.
Output format :
For each test case return the corresponding pattern in the next ‘N’ lines.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 500
Time Limit : 1 sec
2
7
4
1
123
12345
1234567
12345
123
1
1
123
12345
123
1
2
5
6
1
123
12345
123
1
1
123
12345
1234567
12345
123
1
On observing the pattern of first (N/2 +1) rows and last (N/2) rows, can you think of a solution from those observations?
In this pattern, each row starts with 1 and also ends with 1 and it is a diamond pattern so every row contains odd items.
Algorithm :
O(N^2), where ‘N’ is the number of rows.
As we are returning every character of each line one at a time. The upper half diamond contains characters from N/2 to ‘N’ and the lower half diamond contains characters from ‘N’ to N/2. Hence the time required to print ‘N’ lines is N * (N+1)/2, which is equivalent to O(N^2).
O(1)
Constant extra space is used