Number Pattern 1

Easy
0/40
Average time to solve is 10m
profile
Contributed by
49 upvotes
Asked in companies
CognizantAmazonProdapt Solutions

Problem statement

You are given an integer 'N'. Your task is to print the following pattern for the given N number of rows.

For Example: Pattern for N = 4

1
11
111
1111
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases. 

The first line of each test case will contain an integer ‘N’ that is the total number of rows.
Output Format:
For each test case, print the pattern of N lines.

Output for every test case will be printed in a separate line.
Constraints:
1 <= T <= 10
1 <= N <= 10^3

Time Limit: 1 sec
Sample Input 1:
2
1
2
Sample Output 1:
1
1
11
Sample Input 2:
2
3
4
Sample Output 2:
1
11
111
1
11
111
1111
Hint

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

Approaches (2)
Number Pattern 1

The basic idea of this approach is to print the pattern row-wise. For each row, a pattern is observed: 1 is printed 1 time in the 1st row, 2 times in the 2nd row, 3 times in the 3rd row, and so on. So, a pattern is observed that the number of rows determines how many times 1 is printed in each row.


 Here is the algorithm:

 

  1. Run a loop for ‘ROW’ = 0 to ‘N’:
    • Run a loop for ‘COL’ = 0 to equal to ‘ROW’:
      • Print 1.
    • Print a new line.
Time Complexity

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

Because we are using two loops, the outer loop runs for the number of rows given to us, and the inner loop prints the value for every column. Hence the time complexity is O(N^2).

Space Complexity

O(1), 
 

Because we are not using any extra space for finding our answer.

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