Last Updated: 25 Nov, 2020

Game of Numbers

Easy
Asked in companies
HCL TechnologiesSamsung

Problem statement

Ninja loves playing with numbers. So one day, he wants to arrange a few numbers in the ‘N’ number of rows. The first row contains 1 number, the second row has two numbers, and so on.

On row 1, he places ‘1’. From the second row, he puts a number equal to one less than the number of the row at two ends of the row and places zeros in between.

You are given an integer ‘N’ denoting the given number of rows. Can you print the pattern Ninja wants to create?

Pattern for N = 4:

1
11
202
3003
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 a string denoting the required pattern in the following ‘N’ lines.
Constraints:
1 <= T <= 10
1 <= N <= 10^2
Where ‘T’ denotes number of test cases and ‘N’ is the given integer denoting the given number of rows.

Time Limit: 1 sec

Approaches

01 Approach

The key here is to traverse all the lines sequentially, and for each line, we print the required character at the given index.

The steps are as follows:

  • We define an ‘ans’ matrix to store the pattern.
  • We insert ‘1’ in the ‘ans’ matrix.
  • We run a for loop with the condition i is starting from 1 and is less than the number of rows, i.e., n:
  • For row value equal to i and col value equal to i or 0 we insert i in ‘ans’.
  • We run a for loop starting from 1 and less than i.
  • We insert ‘0’ in ‘ans’.
  • Finally, we return ‘ans’.