human
Make all array elements divisible by a number K | Function to make array divisible.
For each element of array how much number to be subtracted to make it divisible by k.
For each element of array how much number to be added to make it divisible by K.
Calculate minimum difference.
Driver Code"
How can the above be solved in Python?
 ... Give me a code to make all array elements divisible by a number K and the other steps required?
assistant
#Here is code to make all array elements divisible by a number K and the other steps required.
 def makeDivisble ( arr , k ) :
	n = len ( arr )
	b1 = [ ]
	b2 = [ ]
	for i in range ( n ) :
		b1 . append ( arr [ i ] % k )
	for j in range ( n ) :
		if ( ( arr [ j ] % k ) != 0 ) :
			b2 . append ( k - ( arr [ j ] % k ) )
		else :
			b2 . append ( 0 )
	c = 0
	mini = float ( ' inf ' )
	suml = 0
	sumr = 0
	index = - 1
	for c in range ( 0 , n + 1 , 1 ) :
		suml = sum ( b1 [ : c + 1 ] )
		sumr = sum ( b2 )
		if suml >= sumr :
			rem = suml - sumr
			if rem < mini :
				mini = rem
				index = c
	return index , mini
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 14 , 4 , 41 , 1 ]
	k = 7
	index , diff = makeDivisble ( arr , k )
	print ( index , diff )