Ninja wants to build a number pattern.
Example For ‘N’ = 4
Pattern:
4444
3444
2344
1234
Your task is to make a program that prints a similar pattern for a given 'N'.
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 one integer, ‘N’, denoting the number of rows.
Output Format:
For each test case print, 'N' strings denoting the pattern.
The output of each test case will be printed on a separate line.
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 200
Time Limit: 1 sec
2
5
2
55555
45555
34555
23455
12345
22
12
Test case 1:
In the first test case, as ‘N’ is equal to ‘5’, We will have to print five lines. We have a print a square-like pattern where the upper right triangle along with the main diagonal will be filled with the input number and the lower-left triangle will be a triangle wherein each row, number of elements increases from ‘1’ to ‘N’ - 1;
Test case 2:
In the second test case, as ‘N’ is equal to ‘2’, We will have to print two lines.
2
1
4
1
4444
3444
2344
1234
Test case 1:
As ‘N’ is equal to ‘1’, we just need to print one line i.e 1.
Derive the next row from the previous one.
We can see that our left-bottom triangle is a pattern of an increasing number and the right upper pattern is a constant pattern. Hence we can first compute a string appending ‘N’ to a local string ‘N’ - 1 time. These will be later used for making the upper-right triangle. Now, we can compute the left-bottom triangle running a while loop with decreasing ‘N’ where we will append ‘N’ to a local string and putting this string in an array of strings. Once, these computations are done, we will join our right triangle with the left triangle by iterating through the array of strings and append the required substring from the precomputed string. While appending, as we are dealing with strings where one number in string format has many digits, we need to know the number of digits present in our original ‘N’ to remove one whole number from the substring.
O(N ^ 2) where N is the given input.
We are precomputing our triangles using ‘N’ iterations. For each row, we are calculating a substring that requires O(N), Hence the overall time complexity is O(N ^ 2).
O(N ^ 2) where N is the number of rows of the array
As we are returning an array of size “N” containing “N string” each of size ‘N’