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
def findIndex ( arr , n , K ) : NEW_LINE INDENT sum = 0 NEW_LINE res = - 1 NEW_LINE mini = 1e9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT temp = sum - arr [ i ] NEW_LINE if ( temp % K == 0 ) : NEW_LINE INDENT if ( res == - 1 or mini > arr [ i ] ) : NEW_LINE INDENT res = i + 1 NEW_LINE mini = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT arr = [ 14 , 7 , 8 , 2 , 4 ] NEW_LINE K = 7 NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N , K ) ) NEW_LINE