Last Updated: 25 Nov, 2020

Number Pattern

Easy
Asked in companies
HSBCIBM

Problem statement

Ninja wants to build a number pattern for the given integer.

For example, If the given integer ‘N’ is 4 

Pattern:

1
23
345
4567

Input Format:

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:

The output of each test case will be 'N' strings denoting the pattern printed for the given ‘N’ number of rows.

Output for each test cases 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.

Constraints:

1 <= T <= 5
1 <= N <= 100

Where ‘T’ is the total number of test cases, ‘N’ is the number of rows in each query.

Approaches

01 Approach

We can see here that we need to print the “N” number of rows for each input.

Each row should start with the corresponding row number, and we need to print the next continuous (row number -1) numbers.

We can take two for loops here in which our first loop will traverse the number of rows.

We will create a array of strings “arr” that will store the elements and return the function array.

In our nested loop, we need to start printing “i” particular numbers beginning with “i” where “i” is the counter for the other loop. We can keep a local variable that will store the value of “i” outside the nested loop each time and store the next “i” integers in the string inside the loop.

 

Algorithm:

 

  • Declare a array of string type with size “N” and local variable “k”
  • Run a loop ‘i’ =  1 to ‘N’ for the number of rows
    • Let ‘k’ = ‘i’
    • Declare a empty string “temp” that will store the number of elements for each row.
    • Run a loop ‘j’ = 1 to ‘i’
      • Concat ‘k’ to the string temp after converting it to a string.
      • Increment ‘k’ by 1.
    • Push the “temp” string to the array arr.
    • Move to the following line
  • Return the array “arr”.