You are given a N \times N grid. You need to fill each cell of the grid with 1 or -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) < 0
For every row - (Sum of values present in the row) x (Product of values present in the row) < 0

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 T, number of testcases. Then the testcases follow.
- Each testcase contains a single integer N

------ Output Format ------ 

- For each test case, print the grid of size N \times N . Print N lines where each line will contain N integers ( 1 or -1 ) separated by a space.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$2 ≤ 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 1 ------ 
Test Case $1$: 
Consider the given output :
- For each row, sum of the elements is $-2$. 
- For each column, sum of the elements is $-2$.
- For each row, product of the elements is $1$.
- For each column, product of the elements is $1$. 

Clearly, both the conditions are satisfied.

Test Case $2$: 
Consider the given output :
- For each row, sum of the elements is $1$. 
- For each column, sum of the elements is $1$.
- For each row, product of the elements is $-1$.
- For each column, product of the elements is $-1$. 

Clearly, both the conditions are satisfied.

Difficulty: EASY
Tags: Constructive algorithms

Mathematics

Ad-hoc

Starter code:

Solutions:
t = int(input())
for dhfu in range(t):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			print('-1 ' * n)
	else:
		for i in range(n):
			print('1 ' * i, end='')
			print('-1 ', '1 ' * (n - i - 1))


for _ in range(int(input())):
	n = int(input())
	if n % 2:
		for i in range(n):
			for j in range(n):
				if j == i:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()
	else:
		for i in range(n):
			print('-1 ' * n)


for i in range(int(input())):
	n = int(input())
	if n % 2 == 1:
		for i in range(n):
			print('1 ' * i + '-1', '1 ' * (n - i - 1))
	else:
		for i in range(n):
			print('-1 ' * n)


t = int(input())
for i in range(t):
	n = int(input())
	rows = n
	columns = n
	arr = [[-1 for j in range(columns)] for k in range(rows)]
	if n > 2:
		for j in range(rows):
			for k in range(columns):
				if j == k:
					arr[j][k] = -1
				else:
					arr[j][k] = 1
	for row in arr:
		print(*row)


t = int(input())
while t:
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				print(-1, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()
	t -= 1


t = int(input())
while t > 0:
	n = int(input())
	l = []
	if n % 2 == 1:
		for i in range(n * n):
			if i % (n + 1) == 0:
				l.append(-1)
			else:
				l.append(1)
	if n % 2 == 0:
		for i in range(n * n):
			l.append(-1)
	for i in range(n * n):
		if i % n == 0:
			print(*l[i:n + i], sep=' ')
	t -= 1


a = int(input(''))
for i in range(a):
	x = int(input(''))
	if x % 2 == 0:
		for i in range(x):
			for j in range(x):
				print(-1, end=' ')
			print()
	else:
		for h in range(x):
			for k in range(x):
				if h == k:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()


t = int(input())
for _ in range(t):
	n = int(input())
	row = []
	s = ' '
	if n % 2 == 0:
		for i in range(n):
			row.append('-1')
		for j in range(n):
			print(s.join(row))
	else:
		for i in range(n):
			row.append('1')
		for k in range(n):
			row[k] = '-1'
			print(s.join(row))
			row[k] = '1'


def func1():
	n = int(input())
	row = []
	str1 = ' '
	if n % 2 == 0:
		for i in range(n):
			row.append('-1')
		for j in range(n):
			print(str1.join(row))
	else:
		for i in range(n):
			row.append('1')
		for k in range(n):
			row[k] = '-1'
			print(str1.join(row))
			row[k] = '1'
t = int(input())
while t > 0:
	func1()
	t = t - 1


T = int(input())
for _ in range(T):
	N = int(input())
	if N % 2 == 0:
		for i in range(N):
			print('-1 ' * N)
	else:
		for i in range(N):
			s = ''
			for j in range(N):
				if i == j:
					s += '-1 '
				else:
					s += '1 '
			print(s)


import random

def check_condition(lis):
	pcount = lis.count(1)
	ncount = lis.count(-1)
	if pcount < ncount and ncount % 2 == 0:
		return 1
	elif pcount > ncount and ncount % 2 == 1:
		return 1
	else:
		return 0

def fill_matrix(mat, num):
	for m in range(num, len(mat)):
		if num != m:
			mat[m].append(mat[num][m])
t = int(input())
for i in range(t):
	n = int(input())
	matrix = [list() for q in range(n)]
	for j in range(n):
		test = 0
		while 1:
			for k in range(j, n):
				matrix[j].append(random.choice([1, -1]))
			test = check_condition(matrix[j])
			if test == 1:
				fill_matrix(matrix, j)
				break
			else:
				del matrix[j][j:n]
	for h in range(n):
		print(' '.join(map(str, matrix[h])))


for t in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				print(-1, end=' ')
			print()
	else:
		for k in range(n):
			for l in range(n):
				if k == l:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()


T = int(input())
for _ in range(0, T):
	n = int(input())
	if n % 2 == 0:
		for i in range(0, n):
			print('-1 ' * n)
	else:
		a = -1
		for i in range(0, n):
			for w in range(0, n):
				if i == w:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()


for x in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				print(-1, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					print(-1, end=' ')
				else:
					print(1, end=' ')
			print()


for _ in range(int(input())):
	N = int(input())
	if N % 2 == 0:
		for i in range(N):
			print('-1 ' * N)
	else:
		a = ['-1 ']
		for i in range(N - 1):
			a.append('1 ')
		for i in range(N):
			print(''.join(a))
			a.insert(0, '1 ')
			a.pop(-1)


for i in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			print('-1 ' * n)
	if n % 2 != 0:
		for i in range(n):
			l = list('1') * n
			l[i] = '-1'
			print(' '.join(l))


t = int(input(''))
if 1 <= t <= 10:
	for m in range(t):
		n = int(input(''))
		if 2 <= n <= 500:
			if n % 2 == 0:
				for i in range(n):
					for j in range(n):
						print(-1, end=' ')
					print()
			else:
				for i in range(n):
					for j in range(n):
						if i == j:
							print(-1, end=' ')
						else:
							print(1, end=' ')
					print()


t = int(input())
high = 1
low = -1
for i in range(t):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				print(low, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					print(low, end=' ')
				else:
					print(high, end=' ')
			print()


t = input()
for c in range(0, int(t)):
	n = input()
	out = []
	if int(n) % 2 == 0:
		for x in range(0, int(n)):
			temp = '-1 ' * int(n)
			out.append(temp)
			print(temp)
	else:
		for y in range(0, int(n)):
			temp = ''
			for z in range(0, int(n)):
				if y == z:
					temp = temp + '-1 '
				else:
					temp = temp + '1 '
			print(temp)


for i in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			print('-1 ' * n)
	elif n % 2 == 1:
		for i in range(n):
			print('1 ' * i, '-1 ', '1 ' * (n - 1 - i))


tests = int(input())
for i in range(tests):
	n = int(input())
	for j in range(n):
		temp = [-1] * n
		if n % 2 != 0:
			temp = [1] * n
			temp[j] *= -1
		print(*temp)


n = int(input())
for i in range(n):
	a = int(input())
	if a % 2 == 0:
		for i in range(a):
			for j in range(a):
				print('-1', end=' ')
			print('\n')
	else:
		c = 0
		for i in range(a):
			for j in range(a):
				if c == j:
					print('-1', end=' ')
				else:
					print('1', end=' ')
			c += 1
			print('\n')


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		print('-1 -1\n-1 -1')
	else:
		for i in range(n):
			sub = ['1'] * n
			sub[i] = '-1'
			print(' '.join(sub))


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	if n % 2 == 0:
		for j in range(n):
			arr.append('-1 ' * n)
	else:
		for j in range(n):
			tmp = ''
			for k in range(n):
				if j == k:
					tmp += '-1 '
				else:
					tmp += '1 '
			arr.append(tmp)
	for strr in arr:
		print(strr)


def grid(y):
	if y % 2 == 0:
		for i in range(y):
			print('-1 ' * y)
	else:
		val = y - 1
		end = 0
		for i in range(y):
			print('-1 ' * val, '1', '-1 ' * end)
			val -= 1
			end += 1
x = int(input())
for _ in range(x):
	y = int(input())
	grid(y)


def output(inp):
	list1 = []
	k = 0
	if inp % 2 == 0:
		for i in range(inp):
			for j in range(inp):
				print(-1, end=' ')
			print()
	else:
		for i in range(inp):
			for j in range(inp):
				if j == k:
					print(1, end=' ')
				else:
					print(-1, end=' ')
			print()
			k = k + 1
n = int(input())
for _ in range(n):
	inp = int(input())
	output(inp)


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		for _ in range(n):
			print(-1, -1, sep=' ')
	else:
		l = [-1 if i < 1 else 1 for i in range(n)]
		for _ in range(n):
			print(*l)
			l = l[-1:] + l[:-1]


def abc(n):
	l1 = []
	for i in range(0, n):
		l2 = [None] * n
		for j in range(0, n):
			if i == j:
				l2[j] = -1
			else:
				l2[j] = 1
		l1.append(l2)
	return l1
for t in range(int(input())):
	l1 = []
	l2 = []
	n = int(input())
	if n > 2:
		x = abc(n)
		for i in range(0, len(x)):
			for j in range(0, len(x[i])):
				print(x[i][j], end=' ')
			print()
	else:
		l2 = [-1, -1]
		l1.append(l2)
		l1.append(l2)
		for i in range(0, len(l1)):
			for j in range(0, len(l1[i])):
				print(l1[i][j], end=' ')
			print()


cases = int(input())
for i in range(cases):
	n = int(input())
	if n == 2:
		print('-1 -1\n-1 -1')
	else:
		for a in range(n):
			for b in range(n):
				if a == b:
					print('-1', end=' ')
				else:
					print('1', end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	if not n % 2:
		print(('-1 ' * n + '\n') * n)
	else:
		count = 1
		for i in range(n):
			print('1 ' * i, '-1 ', '1 ' * (n - 1 - i), sep='')


t = int(input())
for i in range(t):
	n = int(input())
	for j in range(n):
		for k in range(n):
			if n % 2 == 0:
				a = -1
			elif n % 2 == 1 and k == j:
				a = -1
			else:
				a = 1
			print(a, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	for i in range(n):
		for x in range(n):
			if n % 2 == 0:
				a = -1
			elif n % 2 == 1 and x == i:
				a = -1
			else:
				a = 1
			print(a, end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			a = '-1 ' * n
			print(a[:-1])
	else:
		for i in range(n):
			a = ''
			for j in range(n):
				if j == i:
					a += '-1 '
				else:
					a += '1 '
			print(a[:-1])


for _ in range(int(input())):
	n = int(input())
	arr = []
	if n % 2 == 0:
		l = [[-1 for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print()
	else:
		l = [[-1 for i in range(n)] for j in range(n)]
		for i in range(n):
			for j in range(n):
				if i == j:
					l[i][j] = 1
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print()


for tc in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		lst = [-1] * n
		for i in range(n):
			print(*lst)
	else:
		for i in range(n):
			lst = [1] * i + [-1] + [1] * (n - i - 1)
			print(*lst)


def even(n):
	l = [[-1 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()

def odd(n):
	l = [[1 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				l[i][j] = -1
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()
for _ in range(int(input())):
	n = int(input())
	if n & 1 == 0:
		even(n)
	else:
		odd(n)


from sys import stdin
for _ in range(int(stdin.readline())):
	lenGrid = int(stdin.readline())
	if lenGrid > 2:
		lenGridx = lenGrid * lenGrid
		grid = [1 for _ in range(lenGridx)]
		for i in range(0, lenGridx, lenGrid + 1):
			grid[i] = -1
		for i in range(0, lenGridx, lenGrid):
			for j in range(i, i + lenGrid):
				print(grid[j], end=' ')
			print
	else:
		print(-1, -1, '\n', -1, -1)


n = int(input())
A = []
for i in range(0, n):
	x = int(input())
	s = ''
	if x % 2 == 0:
		for j in range(0, x):
			for k in range(0, x):
				s = s + '-1 '
			s = s + '\n'
	else:
		for j in range(0, x):
			for k in range(0, x):
				if j != k:
					s = s + '1 '
				else:
					s = s + '-1 '
			s = s + '\n'
	A.append(s)
for i in A:
	print(i)


for _ in range(int(input())):
	n = int(input())
	print('\n'.join(list((' '.join(list(('-1' for _ in range(n)))) for _ in range(n))))) if n % 2 == 0 else print('\n'.join(list((' '.join(list(('-1' if j == i else '1' for j in range(n)))) for i in range(n)))))


t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n):
		print()
		for j in range(n):
			if n % 2 == 0:
				print(-1, end=' ')
			elif i == j:
				print(-1, end=' ')
			else:
				print(1, end=' ')


for i in range(int(input())):
	X = int(input())
	if X % 2 == 0:
		S = '-1 ' * X
		for j in range(0, X):
			print(S)
	else:
		for j in range(0, X):
			S = '1     ' * j + '-1 ' + '1 ' * (X - j - 1)
			print(S)


m = int(input())
l = []
for x in range(m):
	l.append(int(input()))

def grid(n):
	if n % 2 == 0:
		for x in range(n):
			print('-1 ' * n)
	else:
		i = 0
		for x in range(n):
			if i == 0:
				print('1', '-1 ' * (n - 1))
			elif i == n - 1:
				print('-1 ' * (n - 1), '1')
			else:
				print('-1 ' * i, '1', '-1 ' * (n - 1 - i))
			i = i + 1
for x in l:
	grid(x)