human
Minimum operations required to make each row and column of matrix equals | Function to find minimum operation required to make sum of each row and column equals.
Initialize the sumRow [ ] and sumCol [ ] array to 0.
Calculate sumRow [ ] and sumCol [ ] array.
Find maximum sum value in either row or in column.
Find minimum increment required in either row or column.
Add difference in corresponding cell , sumRow [ ] and sumCol [ ] array.
Update the count variable.
If ith row satisfied , increment ith value for next iteration.
If jth column satisfied , increment jth value for next iteration.
Utility function to print matrix.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to minimum operations required to make each row and column of matrix equals and the other steps required.
 def findMinOpeartion ( matrix , n ) :
	sumRow = [ 0 ] * n
	sumCol = [ 0 ] * n
	for i in range ( n ) :
		for j in range ( n ) :
			sumRow [ i ] += matrix [ i ] [ j ]
			sumCol [ j ] += matrix [ i ] [ j ]
	maxSum = 0
	for i in range ( n ) :
		maxSum = max ( maxSum , sumRow [ i ] )
		maxSum = max ( maxSum , sumCol [ i ] )
	count = 0
	i = 0
	j = 0
	while i < n and j < n :
		diff = min ( maxSum - sumRow [ i ] , maxSum - sumCol [ j ] )
		matrix [ i ] [ j ] += diff
		sumRow [ i ] += diff
		sumCol [ j ] += diff
		count += diff
		if ( sumRow [ i ] == maxSum ) :
			i += 1
		if ( sumCol [ j ] == maxSum ) :
			j += 1
	return count
def printMatrix ( matrix , n ) :
	for i in range ( n ) :
		for j in range ( n ) :
			print ( matrix [ i ] [ j ] , end = " ▁ " )
		print ( )
if __name__ == " _ _ main _ _ " :
	matrix = [ [ 1 , 2 ] , [ 3 , 4 ] ]
	print ( findMinOpeartion ( matrix , 2 ) )
	printMatrix ( matrix , 2 )