Last Updated: 25 Nov, 2020

Number Pattern 3

Easy
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
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

Approaches

01 Approach

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.