Last Updated: 25 Nov, 2020

Number Pattern

Easy
Asked in companies
Capegemini Consulting India Private LimitedNagarro Software

Problem statement

Ninja is given a pattern. Now he is asked to print the same pattern for any given 'N' number of rows.

For example Pattern for 'N' = 4 will be-

1234
123
12
1

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 a single integer ‘N’ denoting the number of rows for which pattern needs to be printed.

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 case will be printed on a separate line.

Constraints:

1 <=T <= 5 
1 <= N <= 500 

Where ‘T’ is the total number of test cases and ‘N’ is the given number of rows for which pattern needs to be printed.

Time limit: 1 sec

Approaches

01 Approach

We can observe that there is an ‘N’ number of rows (where ‘N’ is the total number of rows to be printed). Each row exactly contains the ‘row’ number of columns (where ’row’ is the current row number). And for each row in each column ‘col’  gets printed (where ’col’ is the current column number).

 

 Algorithm:

 

  • Declare an array of strings for storing the pattern.
  • Declare a variable ‘row’ for keeping track of the number of rows and initialize its value to ‘N’ where ‘N’ is the given number of rows and ‘col’ for keeping track of the number of columns.
  • Run an outer loop from ‘row’ = ‘N to ‘row’ = ‘1’.
    • Run an inner loop ‘col’ = 1 to ‘col’ = ‘row’ (where ‘row’ is the current row number).
      • Inside this loop, we append the value of ‘col’ in the current row of our vector. (where ‘col’ is the current column number).
    • After the loop, we move to a new row of our result vector.