human
Find the index of the smallest element to be removed to make sum of array divisible by K | Function to find index of the smallest array element required to be removed to make sum divisible by K.
Stores sum of array elements.
Stores index of the smallest element removed from the array to make sum divisible by K.
Stores the smallest element removed from the array to make sum divisible by K.
Traverse the array , arr [ ].
Update sum.
Traverse the array arr [ ].
Calculate remaining sum.
If sum is divisible by K.
If res == - 1 or mini is greater than arr [ i ].
Update res and mini.
Driver Code"
Solution in Python:
assistant
#Here is code to find the index of the smallest element to be removed to make sum of array divisible by K and the other steps required.
 def findIndex ( arr , n , K ) :
	sum = 0
	res = - 1
	mini = 1e9
	for i in range ( n ) :
		sum += arr [ i ]
	for i in range ( n ) :
		temp = sum - arr [ i ]
		if ( temp % K == 0 ) :
			if ( res == - 1 or mini > arr [ i ] ) :
				res = i + 1
				mini = arr [ i ]
	return res ;
arr = [ 14 , 7 , 8 , 2 , 4 ]
K = 7
N = len ( arr )
print ( findIndex ( arr , N , K ) )