Minimum prefix increments required to make all elements of an array multiples of another array | 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 ; Input arrays A [ ] and B [ ] ; Length of arrays
function MinimumMoves ( A , B , N ) { let totalOperations = 0 ; let carry = 0 ; let K = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let nearestMultiple = ( Math . ceil ( ( A [ i ] + carry ) / ( B [ i ] ) ) * B [ i ] ) ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } let A = [ 3 , 4 , 5 , 2 , 5 , 5 , 9 ] ; let B = [ 1 , 1 , 9 , 6 , 3 , 8 , 7 ] ; let N = A . length ; document . write ( MinimumMoves ( A , B , N ) + " " ) ;