Binary Pattern

Easy
0/40
Average time to solve is 10m
profile
Contributed by
25 upvotes
Asked in companies
CognizantSAP LabsTata Consultancy Services (TCS)

Problem statement

You have been given an input integer 'N'. Your task is to print the following binary pattern for it.

Example

Pattern for 'N' = 4

1111
000
11
0

The first line contains 'N' 1s. The next line contains 'N' - 1 0s. Then the next line contains 'N' - 2 1s and so on.

Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line of input contains an integer 'T' that denotes the number of test cases.

The first line of each test case contains a single integer 'N'.
Output format :
For each test case, print 'N' lines containing the pattern, each of the 'I-th' lines should contain 'N' - 'I' + 1 number depending on the 'I' value. 
Constraints :
1 <= T <= 10
1 <= N <= 10^3

Time Limit: 1 sec
Sample Input 1:
2
1
2
Sample Output 1:
1
11
0
Sample Input 2:
2
3
5
Sample Output 2:
111
00
1
11111
0000
111
00
1
Hint

Can you use for loops?

Approaches (1)
Iterative

In the pattern, every even-numbered line is filled with 0's, and the odd-numbered line is filled with 1's and we have total N lines. 

We can run a for loop from 0 to N for each line and inside that for loop we can run again another for loop till the N - i times where ‘i’ is the line number. respective of line number we can print the 0 or 1.  

Time Complexity

O(N^2) where N is the input integer. 

 

As we are running 2 loops from 0 to N, there will be N^2 steps hence overall time complexity will be O(N^2).

Space Complexity

O(1)

 

There will not be any extra space usage.

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