Minimum prefix increments required to make all elements of an array multiples of another array | Python3 program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays
from math import ceil , floor NEW_LINE def MinimumMoves ( A , B , N ) : NEW_LINE INDENT totalOperations = 0 NEW_LINE carry = 0 NEW_LINE K = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT nearestMultiple = ceil ( ( A [ i ] + carry ) / B [ i ] ) * B [ i ] NEW_LINE K = nearestMultiple - ( A [ i ] + carry ) NEW_LINE totalOperations += K NEW_LINE carry += K NEW_LINE DEDENT return totalOperations NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 4 , 5 , 2 , 5 , 5 , 9 ] NEW_LINE B = [ 1 , 1 , 9 , 6 , 3 , 8 , 7 ] NEW_LINE N = len ( A ) NEW_LINE print ( MinimumMoves ( A , B , N ) ) NEW_LINE DEDENT