Number Pattern

Easy
0/40
Average time to solve is 15m
profile
Contributed by
25 upvotes
Asked in companies
Nagarro SoftwareCapegemini Consulting India Private Limited

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
Detailed explanation ( Input/output format, Notes, Images )

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

Sample Input1 :

1 
5

Sample Output1 :

12345
1234
123
12
1

Explanation of Sample Input 1 :

For test case 1:
We print the given pattern for the given 5 rows where the first row prints from column number 1 to 5 and no of columns reduces by 1 for each consecutive row.

Sample Input2 :

1
3

Sample Output2 :

123
12
1

Explanation of Sample Input 2 :

For test case 1:

We print the given pattern for the given 3 rows where the first row prints from column number 1 to 3 and no of columns reduces by 1 for each consecutive row.
Hint

Try to make relation between number of columns in particular row.

Approaches (1)
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.
Time Complexity

O(N ^ 2), where ‘N’ is the given number of rows.

 

As we will be traversing the given number of rows and for each row number, we will be traversing it from 1 to that row number for printing the columns of the pattern our overall time complexity is O(N ^ 2).

Space Complexity

O(N ^ 2) where ‘N’ is the given number of rows.

 

As we are using an array of strings to store the pattern.

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