Print the Pattern

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
66 upvotes
Asked in companies
Citi BankCIS - Cyber InfrastructureDell Technologies

Problem statement

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

Note:

There is only one space between the values of each column in a row.

For example, Pattern for ‘N’ = 5 will be.
1 2 3 4 5 
11 12 13 14 15 
21 22 23 24 25 
16 17 18 19 20 
6 7 8 9 10 
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.

The output of each test case will be printed on a separate line.

Constraints:

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

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 Output2 :

1 2 3 4 5 
11 12 13 14 15 
21 22 23 24 25 
16 17 18 19 20 
6 7 8 9 10 

Explanation of Sample Input 1:

For test case 1:
We print the given pattern for the given 5 rows where each row has different values in increasing order with a difference in the value of 1 between each element and 1 space between each column in a row.

Sample Input2 :

1
4

Sample Output2 :

1 2 3 4
9 10 11 12
13 14 15 16
5 6 7 8

Explanation of Sample Input 2:

For test case 1:
We print the given pattern for the given 4 rows where each row has different values in increasing order with a difference of 1 in the value of 1 between each element and 1 space between each column in a row.
Hint

Try to make the relationship between the row numbers and the initial value in each row.

Approaches (1)
Observation

We can observe that there are ‘N’ numbers of rows (where ‘N’ is the total number of rows to be printed). Each row contains a different value in the first column and its value increments by 1 for each subsequent column.

 

Algorithm:

 

  • Declare an array of strings for storing the pattern.
  • Declare a variable ‘startValue’  and initialize its value to 1.
  • Declare a variable ‘row’ for keeping track of the number of rows
  • Run an outer loop from ‘row’ = ‘1 to ‘row’ = ‘N’.
    • Declare an empty string for storing the values in the current row.
    • Run an inner loop for printing values from ‘col’ = ‘startvalue’ to ‘col’= ‘startvalue + N - 1’.
    • For every row, we need to set the value of ‘startvalue‘ according to the following conditions:
      • If ‘row’ == (‘N’ + 1) / 2
        • If ‘N’ % 2 ! = 0
          • Set ‘startValue’ = ‘N’ * (‘N’ - 2) + 1
        • Else
          • Set ‘startValue’ = ‘N’ * (‘N’ - 1) + 1
      • Else If ‘row’ > (‘N’ + 1) / 2
        • Set ‘startValue’ = ‘startValue’ - 2 * ‘N’
      • Else
        • Set ‘startValue’ = ‘startValue’ + 2 * ‘N’
    • After the loop, we append the string to our new row and move to a new row of our array.
Time Complexity

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

 

As we will be traversing the given ‘N’ number of rows and for each row, we will be traversing it for ‘N’ no of columns so our complexity will be O(N ^ 2).

Space Complexity

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

 

As we are using an array of strings that takes O(N ^ 2) space.

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