human
Can you provide a code to make all array elements equal with minimum cost and the other steps required given the following:
Make all array elements equal with minimum cost | This function assumes that a [ ] is sorted . If a [ ] is not sorted , we need to sort it first ..
If there are odd elements , we choose middle element.
If there are even elements , then we choose the average of middle two ..
After deciding the final value , find the result ..
Driver code"
Solution in Python:
assistant
#Here is code to make all array elements equal with minimum cost and the other steps required.
 def minCostToMakeElementEqual ( a ) :
	l = len ( a )
	if ( l % 2 == 1 ) :
		y = a [ l // 2 ]
	else :
		y = ( a [ l // 2 ] + a [ ( l - 2 ) // 2 ] ) // 2
	s = 0
	for i in range ( l ) :
		s += abs ( a [ i ] - y )
	return s
a = [ 1 , 100 , 101 ]
print ( minCostToMakeElementEqual ( a ) )