Number Pattern

Easy
0/40
Average time to solve is 15m
profile
Contributed by
25 upvotes
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
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 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.

Sample Input 1:

2
3
5

Sample Output 1:

1
23
345
1
23
345
4567
56789

Explanation of Sample Input 1:

Test case 1:

In the first test case, as ‘N’ is equal to 3, We will have to print three lines. Each line starts with its row number and prints next (row number - 1) numbers.

As in this example:
The first row starts with “1” as the row number is one and print only “1” as “row number - 1“ is 0
For the second row, it starts with two and prints one more number that is 3.Similarly, the third line starts with 3 and ends at 5.

Test case 2:

In the second test case, as “N” is equal to “5”, We will have to print five lines. Each line starts with its row number and prints next (row number - 1) numbers.

As in this example:
The first row starts with 1 as the row number is one and print only 1 as “row number - 1“ is 0
The second row starts with two and prints one more number that is “3”.
Similarly, the 5th row starts with “5” and ends at “9”.

Sample Input 2:

1
1

Sample Output 2:

1

Explanation of Sample Input 2:

Test case 1:
As ‘N’ is equal to ‘1’, we just need to print one line i.e 1. 
Hint

Observe the row number carefully.

Approaches (1)
Observe the Pattern

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”.
Time Complexity

O(N ^ 2) where N is the number of rows of the array.

 

We are traversing “N” rows in each test case. In each row, we are printing “ row number elements”. So our nested loop with the other loop will run (N * (N+1))/2 times. Hence the overall time complexity is O(N ^ 2).

Space Complexity

O(N ^ 2)  where N is the number of rows of the array

 

As we are returning an array of size “N” containing “N string” each of size beginning from “1” to “N”.

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