Number Pattern 3

Easy
0/40
Average time to solve is 10m
profile
Contributed by
33 upvotes
Asked in companies
HCL TechnologiesIHS MarkitTravClan

Problem statement

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
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 10
1 <= N <= 10^3

Where ‘N’ is the number of lines.

Time Limit: 1 sec
Sample Input 1:
2    
3
1 
Sample Output 1:
1
11
121
1
Explanation for Sample Input 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.
Sample Input 2:
1
2
4
Sample Output 2:
1
11
1
11
121
1221
Hint

Can you print the table details of each row one at a time?

Approaches (1)
Brute Force

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:

  • We define a ‘RESULT’ matrix.
  • We traverse from ‘i’ = 1 to ‘N’, indicating the row number
    • We then traverse from ‘j’ = 1 to ‘i’, indicating the tables of each line.
      • If it is the first or the last character, that is ‘j’ == 1 or ‘j’ == ‘i’, we insert ‘1’ in the ‘RESULT’ matrix.
      • Else we insert ‘2’ in the ‘RESULT’ matrix.
Time Complexity

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).

Space Complexity

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).

Code Solution
(100% EXP penalty)
Number Pattern 3
Full screen
Console