Given a matrix A of dimensions NxM. Check whether the sum of the i^{th} row is equal to the sum of the i^{th} column.
Note: Check only up to valid row and column numbers i.e if the dimensions are 3x5, check only for the first 3 rows and columns, i.e. min(N, M).
Example 1:
Input:
N=2,M=2
A=[[1,2],[2,1]]
Output:
1
Explanation:
The sum of 1st row is equal to sum of
1st column and also sum of 2nd row is equal 
to the sum of 2nd column.So, Answer is 1.
Example 2:
Input:
N=1,M=3
A=[[5],[0],[0]]
Output:
1
Explanation:
The sum of 1st column is equal
to the sum of 1st row.Thus,answer is 1.
(We do not check for the 2nd and 3rd rows
because there are no 2nd and 3rd columns.)
Your Task:
You don't need to read input or print anything. Your task is to complete the function sumOfRowCol() which takes two integers N, M and a 2D array A as input parameters and returns 1 if all the valid sum of rows is equal to the valid sum of columns. Otherwise, returns 0.
Expected Time Complexity:O(N*M)
Expected Auxillary Space:O(min(N,M))
 
Constraints:
1<=N,M,A[i][j]<=10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3

class Solution:
    def sumOfRowCol(self,N,M,A):
        #code here

Solutions:
class Solution:

	def sumOfRowCol(self, N, M, A):
		r_sum = [sum(row) for row in A]
		col_sums = [0] * M
		for i in range(N):
			for j in range(M):
				col_sums[j] += A[i][j]
		if r_sum == col_sums:
			return 1
		else:
			return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		r = []
		c = []
		for i in range(N):
			for j in range(M):
				r.append(A[0][j])
				c.append(A[i][0])
		if sum(r) == sum(c):
			return 1
		else:
			return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		if N == 1 and M == 1:
			return 1
		r = [0] * N
		c = [0] * M
		for i in range(N):
			csum = 0
			for j in range(M):
				c[j] += A[i][j]
				csum += A[i][j]
			r[i] += csum
		mini = min(N, M)
		for i in range(mini):
			if r[i] != c[i]:
				return 0
		else:
			return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		for i in range(min(N, M)):
			s1 = 0
			for j in range(N):
				s1 += A[j][i]
			s2 = 0
			for j in range(M):
				s2 += A[i][j]
			if s1 != s2:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		row = [-1] * min(N, M)
		col = [-1] * min(N, M)
		if N < M:
			for i in range(N):
				row_sum = 0
				for j in range(M):
					row_sum += A[i][j]
				row[i] = row_sum
			for i in range(N):
				col_sum = 0
				for j in range(N):
					col_sum += A[j][i]
				col[i] = col_sum
		else:
			for i in range(M):
				row_sum = 0
				for j in range(M):
					row_sum += A[i][j]
				row[i] = row_sum
			for i in range(M):
				col_sum = 0
				for j in range(N):
					col_sum += A[j][i]
				col[i] = col_sum
		for i in range(len(row)):
			if row[i] != col[i]:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		row = [0] * N
		col = [0] * M
		for i in range(N):
			summ = 0
			for j in range(M):
				summ += A[i][j]
			row[i] = summ
		for i in range(M):
			summ = 0
			for j in range(N):
				summ += A[j][i]
			col[i] = summ
		for i in range(min(N, M)):
			if row[i] != col[i]:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		min_ = min(N, M)
		for i in range(min_):
			row_sum = 0
			col_sum = 0
			for j in range(M):
				row_sum += A[i][j]
			for j in range(N):
				col_sum += A[j][i]
			if row_sum != col_sum:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		lst = []
		for i in zip(*A):
			lst.append(sum(i))
		c = 0
		lst1 = []
		for i in A:
			lst1.append(sum(i))
		return 1 if lst == lst1 else 0


import numpy as np

class Solution:

	def sumOfRowCol(self, N, M, A):
		(mat, maT) = (np.array(A), np.array(A).T)
		for i in range(min(M, N)):
			if sum(mat[i]) != sum(maT[i]):
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		n = min(N, M)
		rows = [sum(row) for row in A]
		cols = [sum(col) for col in zip(*A)]
		return int(all((rows[i] == cols[i] for i in range(n))))


class Solution:

	def sumOfRowCol(self, N, M, A):
		c = min(N, M)
		for i in range(0, c):
			k1 = k2 = 0
			for j in range(0, M):
				k1 += A[i][j]
			for j in range(0, N):
				k2 += A[j][i]
			if k1 != k2:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		l = []
		count = 0
		for i in range(min(N, M)):
			for j in range(M):
				count += A[i][j]
			l.append(count)
			count = 0
		for j in range(min(N, M)):
			for i in range(N):
				count += A[i][j]
			if count != l[j]:
				return 0
			count = 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		sum1 = 0
		for i in range(N):
			sum1 = sum(A[i])
			a = 0
			for j in range(N):
				a += A[j][i]
			if sum1 != a:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		if N > M:
			return 0
		flag = False
		for i in range(N):
			rsum = sum(A[i])
			ans = 0
			for j in range(N):
				ans += A[j][i]
			if ans != rsum:
				flag = True
		if flag:
			return 0
		else:
			return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		x = min(N, M)
		r = []
		for i in range(x):
			r.append(sum(A[i]))
		c = []
		for i in range(x):
			current = 0
			for j in range(len(A)):
				current += A[j][i]
			c.append(current)
		if c == r:
			return 1
		return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		if M == 1 and N == 1:
			return 1
		if M == 1 or N == 1:
			return 0
		if M > N:
			M = N
		for i in range(M):
			sum1 = 0
			sum2 = 0
			for j in range(M):
				sum1 = sum1 + A[i][j]
				sum2 = sum2 + A[j][i]
			if sum1 != sum2:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		n = min(N, M)
		i = 0
		while i < n:
			sum1 = 0
			sum2 = 0
			for j in range(M):
				sum1 += A[i][j]
			for k in range(N):
				sum2 += A[k][i]
			if sum1 != sum2:
				return 0
			i += 1
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		sum1 = 0
		sum2 = 0
		count = 0
		if N < M:
			n = N
		else:
			n = M
		for i in range(N):
			sum1 = sum1 + A[i][0]
		for i in range(M):
			sum2 = sum2 + A[0][i]
		if sum1 == sum2:
			count = 1
		return count


class Solution:

	def sumOfRowCol(self, N, M, A):
		col = 0
		row = 0
		r = []
		c = []
		for x in range(N):
			for y in range(M):
				row += A[x][y]
			r.append(row)
			row = 0
		for x in range(M):
			for y in range(N):
				col += A[y][x]
			c.append(col)
			col = 0
		if N > M:
			for x in range(M, N):
				c.append(0)
		if M > N:
			for x in range(N, M):
				r.append(0)
		if r == c:
			return 1
		return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		rowSum = 0
		coulmnSum = 0
		i = 0
		n = min(N, M)
		while i < n:
			for j in range(M):
				rowSum += A[i][j]
			for k in range(N):
				coulmnSum += A[k][i]
			if rowSum != coulmnSum:
				return 0
			i += 1
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		mins = min(N, M)
		if N == 1 and M == 1:
			return 1
		elif N == 1 or M == 1:
			return 0
		for x in range(0, mins):
			rows = 0
			col = 0
			for y in range(0, mins):
				rows += A[x][y]
				col += A[y][x]
			if rows != col:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		n = min(N, M)
		sr = []
		for i in range(n):
			sr.append(sum(A[i]))
		sc = []
		col_totals = [sum(x) for x in zip(*A)]
		sc = col_totals[:n]
		for i in range(n):
			if sr[i] != sc[i]:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		A_T = list(map(list, zip(*A)))
		limit = min(N, M)
		for itr in range(limit):
			if sum(A[itr]) != sum(A_T[itr]):
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		d = min(N, M)
		for i in range(d):
			r = 0
			for j in range(M):
				r += A[i][j]
			c = 0
			for k in range(N):
				c += A[k][i]
			if c == r:
				return 1
		return 0
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(N, M) = map(int, input().strip().split(' '))
		A = []
		for i in range(N):
			B = list(map(int, input().strip().split(' ')))
			A.append(B)
		ob = Solution()
		print(ob.sumOfRowCol(N, M, A))


class Solution:

	def sumOfRowCol(self, N, M, A):
		final = []
		for i in range(N):
			sum1 = 0
			for j in range(M):
				sum1 += A[i][j]
			final.append(sum1)
		for i in range(M):
			sum1 = 0
			for j in range(N):
				sum1 += A[j][i]
			if sum1 != final[i]:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		k = 0
		while k < min(N, M):
			sum_row = 0
			sum_column = 0
			i = k
			j = 0
			while j < M:
				sum_row += A[i][j]
				j += 1
			j = 0
			j = k
			i = 0
			while i < N:
				sum_column += A[i][j]
				i += 1
			i = 0
			if sum_row == sum_column:
				res = 1
			else:
				res = 0
			k += 1
		return res


class Solution:

	def sumOfRowCol(self, N, M, A):
		flag = True
		n = min(N, M)
		for i in range(n):
			(row_sum, col_sum) = (0, 0)
			for row_index in range(N):
				row_sum += A[row_index][i]
			for col_index in range(M):
				col_sum += A[i][col_index]
			if row_sum != col_sum:
				flag = False
		if flag:
			return 1
		else:
			return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		sum_row = 0
		sum_column = 0
		for i in range(0, N):
			for j in range(0, M):
				sum_row += A[i][j]
				break
		for j in range(0, M):
			for i in range(0, N):
				sum_column += A[i][j]
				break
		if sum_row == sum_column:
			return 1
		return 0


class Solution:

	def sumOfRowCol(self, N, M, A):
		sum_rows = [0] * N
		sum_cols = [0] * M
		for i in range(N):
			for j in range(M):
				sum_rows[i] += A[i][j]
		for i in range(M):
			for j in range(N):
				sum_cols[i] += A[j][i]
		for i in range(min(len(sum_rows), len(sum_cols))):
			if sum_rows[i] != sum_cols[i]:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		minL = min(N, M)
		SumR = 0
		SumC = 0
		for i in range(minL):
			for j in range(M):
				SumR += A[i][j]
			for j in range(N):
				SumC += A[j][i]
			if SumR != SumC:
				return 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		x = min(N, M)
		flag = 0
		for i in range(x):
			sum1 = 0
			sum2 = 0
			for j in range(M):
				sum1 += A[i][j]
			for j in range(N):
				sum2 += A[j][i]
			if sum1 == sum2:
				flag = 1
		return flag


class Solution:

	def sumOfRowCol(self, N, M, A):
		row_sum = 0
		column_sum = 0
		for i in range(N):
			for x in range(M):
				row_sum += A[i][x]
			for y in range(N):
				column_sum += A[y][i]
			if row_sum != column_sum:
				return 0
			row_sum = 0
			column_sum = 0
		return 1


class Solution:

	def sumOfRowCol(self, N, M, A):
		if N == 1 and M == 1:
			return 1
		if N == 1 or M == 1:
			return 0
		x = min(N, M)
		sum_list = []
		sum_list1 = []
		sum = 0
		for i in range(0, x):
			for j in range(0, x):
				sum += A[i][j]
			sum_list.append(sum)
			sum = 0
		for j in range(0, x):
			for i in range(0, x):
				sum += A[i][j]
			sum_list1.append(sum)
			sum = 0
		for i in range(0, x):
			if sum_list[i] == sum_list1[i]:
				continue
			else:
				return 0
		return 1