human
Minimum operations of given type to make all elements of a matrix equal | Function to return the minimum number of operations required.
Create another array to store the elements of matrix.
If not possible.
Sort the array to get median.
To count the minimum operations.
If there are even elements , then there are two medians . We consider the best of two as answer ..
Return minimum operations required.
Driver code"
Solution in Python:

What is a code to minimum operations of given type to make all elements of a matrix equal and the other steps required?
assistant
#Here is code to minimum operations of given type to make all elements of a matrix equal and the other steps required.
 def minOperations ( n , m , k , matrix ) :
	arr = [ 0 ] * ( n * m )
	mod = matrix [ 0 ] [ 0 ] % k
	for i in range ( 0 , n ) :
		for j in range ( 0 , m ) :
			arr [ i * m + j ] = matrix [ i ] [ j ]
			if matrix [ i ] [ j ] % k != mod :
				return - 1
	arr . sort ( )
	median = arr [ ( n * m ) // 2 ]
	minOperations = 0
	for i in range ( 0 , n * m ) :
		minOperations += abs ( arr [ i ] - median ) // k
	if ( n * m ) % 2 == 0 :
		median2 = arr [ ( ( n * m ) // 2 ) - 1 ]
		minOperations2 = 0
		for i in range ( 0 , n * m ) :
			minOperations2 += abs ( arr [ i ] - median2 ) // k
		minOperations = min ( minOperations , minOperations2 )
	return minOperations
if __name__ == " _ _ main _ _ " :
	matrix = [ [ 2 , 4 , 6 ] , [ 8 , 10 , 12 ] , [ 14 , 16 , 18 ] , [ 20 , 22 , 24 ] ]
	n = len ( matrix )
	m = len ( matrix [ 0 ] )
	k = 2
	print ( minOperations ( n , m , k , matrix ) )