Play with Pattern

Easy
0/40
Average time to solve is 20m
21 upvotes
Asked in company
Tata Consultancy Services (TCS)

Problem statement

Ninja wanted to go to a party along with his friends. However, his mom wanted him to go only if he completes a task assigned by her.

She gave Ninja a value and asked him to print an X-shaped pattern.

Example : Pattern for N = 3 (No. of rows = 5, No. of columns = 5) :

1   1
 2 2
  3
 2 2
1   1

Since Ninja is in a hurry and doesn’t want to be late for the party; he asks you to solve the problem. Can you help solve this problem?

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. The test cases follow.

The first line of each test case contains a single integer ‘N’.
Output Format:
For each test case, print every row in a new line (row elements not separated by space)

Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 50
1 <= N <= 1000

Time Limit: 1 sec
Sample Input 1:
2
3
2
Sample Output 1:
1   1
 2 2
  3
 2 2
1   1

 1 1
  2
 1 1
Explanation for Sample Input 1:
In the first test case, ‘N’ is 3, so print the rows from 1 to ‘ 2*N -1 ’, and if the conditions are followed correctly, the above pattern is printed for N=3.

In the second test case, the value of ‘N’ is 2, so print the rows from 1 to ‘2*N - 1’, and if the conditions are followed correctly, the above pattern is printed for N=2.
Sample Input 2:
2
4
5
Sample Output 2:
1     1
 2   2 
  3 3  
   4   
  3 3  
 2   2 
1     1

1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1
Hint

Check if you can manipulate the nested loops to print the result?

Approaches (1)
Brute Force

The idea is to use nested loops in such a way that yields the answer.

 

The steps are as follows:

  • Initially, take a variable ‘value’ equals 1, which will be used to print all the values of the pattern.
  • The pattern consists of precisely N * 2 - 1 rows and columns. Hence we will run an outer loop to iterate through rows with structure from i=0 to i = N * 2 - 1.
    • Since each row contains exactly N * 2 - 1 columns. Therefore, we will run an inner loop from j=0 to j = N * 2 - 1.
      • Since row and column, the count is the same; both loops will run for the same amount of time.
      • Inside this loop, we can notice that numbers are printed for diagonals only, otherwise, we will print space.
      • For the first diagonal, i.e., when row and column number both are equal. It means to print the value whenever i==j.
      • For the second diagonal i.e. values are printed if (i + j) == 2 * (n - 1).
      • For the remaining values, check if i < N-1. If it is then increment the value, else decrement the value.
    • Print a new line after the execution for each row.
Time Complexity

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

 

We are traversing each row of the matrix (2*N-1) time that takes O(N) time complexity, and inside each loop, we are using an additional loop between the columns for (2*N-1) time, which takes extra time (N). Hence the overall time complexity is O(N^2).

Space Complexity

O(1), no extra space required.

 

As we are not using any extra space except for an array to maintain 26 characters. Hence, the overall space complexity is O(1). 

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