Given an integer N denotes N×N matrix. Initially, each cell of matrix is empty. U r given K tasks. In each task, you are given a cell (i,j) where cell (i,j) represents
ith row and jth column of given matrix.
I/p format
O/p format
Print K space-separated integers denoting numberr of empty cells in matrix.
Constraints
1≤N≤105
1≤K≤1000
1≤i,j≤N
Sample Input
3 3 ==> Matrix Size is 3 and Tasks is 3.
It should be a square matrix like 2 4 6 8...and max can be 100 x 100
Initial Matrix
Final Result
4 2 0
Initially all cells [{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}] are empty.
After first task:
Empty cells are [{2, 2}, {2, 3}, {3, 2}, {3, 3}]. Therefore, answer is 4.
After second task:
Empty cells are [{2, 2}, {3, 2}]. Therefore, answer is 2.
After third task:
No cells remain empty. Therefore, answer is 0.
n, k = map(int, input().split(" "))
inp = []
rows = []
columns = []
left = n*n
for i in range(k):
j, k = map(int, input().split(" "))
if j not in rows:
rows.append(j)
left = left - n + len(columns)
if k not in columns:
columns.append(k)
left = left - n + len(rows)
print(left, end = " ")
Comments
Leave a comment