A high-security meeting has been arranged. Tables for the delegates and security personnel have been arranged. A total of ‘N’ rows of tables has been set up. The first row has one table, the second row has two, and so on. To ensure maximum security, the tables on either end of each row have been assigned for security personnel each. If there is only one table in a row, it will be assigned to a security personnel. The tables assigned for security personnel will host exactly one security personnel each. All the other tables will host two guests each.
You are given an integer ‘N’, which denotes the number of rows., You are supposed to print the table pattern indicating the number of guests or security personnel at each table. In other words, print the number of people in each table.
For example, if the number of rows are 4, the table pattern is as follows:1
11
121
1221
The first line contains ‘T’, denoting the number of test cases.
Each test case contains a single integer ‘N’, denoting the number of rows.
Output Format:
For each test case, print 'N' strings denoting the pattern.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^3
Where ‘N’ is the number of lines.
Time Limit: 1 sec
2
3
1
1
11
121
1
In the first test case, we are required to print three rows of tables. The first row has a single table. It will host security personnel. The second table has two tables. As mentioned in the plan, the end tables should have security personnel, so both tables will have security personnel. The third row hosts security personnel on table 1 and table 3. Table 2 will host guests.
The second test case has only one row. It has a single table and will be assigned to a security personnel.
1
2
4
1
11
1
11
121
1221
Can you print the table details of each row one at a time?
The idea here is to traverse all the rows and, for each row, print the suitable character at any particular table.
The steps are as follows:
O(N^2), where ‘N’ is the number of rows.
In this approach, we are inserting every table detail of each row one at a time. The first row contains one table, the second row two, and so on. Hence time required to print N rows is 1 + 2 + 3 + … + N = (N*(N+1))/2, which is equivalent to O(N^2).
Hence, the overall complexity is O(N^2).
O(N^2), where ‘N’ is the number of rows.
We are using a matrix of ‘N’ rows and ‘N’ columns. Hence, the overall space complexity becomes O(N^2).