The Grid Fill Problem solution codechef- You are given a NN ×× NN grid. You need to fill each cell of the grid with 11 or −1−1. You need to fill the grid in a way such that the following conditions are met :-

The Grid Fill Problem solution codechef

You are given a NN ×× NN grid. You need to fill each cell of the grid with 11 or 1−1. You need to fill the grid in a way such that the following conditions are met :-

  • For every column – (Sum of values present in the column) x (Product of values present in the column) < 00
  • For every row – (Sum of values present in the row) x (Product of values present in the row) < 00

It is guaranteed that there exists at least one way to fill the grid under given constraints such that both the conditions are satisifed. If there exists multiple ways to fill the grid by satisfying both the conditions, you can print any.

Input Format

  • First line will contain TT, number of testcases. Then the testcases follow.
  • Each testcase contains a single integer NN

Output Format

  • For each test case, print the grid of size NN ×× NN . Print NN lines where each line will contain NN integers ( 11 or 1−1 ) separated by a space.

Constraints

  • 1T101≤T≤10
  • 2N5002≤N≤500

Sample Input 1 

2
2
3

Sample Output 1 

-1 -1
-1 -1 
1 -1 1
-1 1 1
1 1 -1

Explanation

Test Case 11: Consider the given output :

  • For each row, sum of the elements is 2−2.
  • For each column, sum of the elements is 2−2.
  • For each row, product of the elements is 11.
  • For each column, product of the elements is 11.

Clearly, both the conditions are satisfied.

Test Case 22: Consider the given output :

  • For each row, sum of the elements is 11.
  • For each column, sum of the elements is 11.
  • For each row, product of the elements is 1−1.
  • For each column, product of the elements is 1−1.

Clearly, both the conditions are satisfied.

Leave a Comment

Your email address will not be published.