Minimum count of increment of K size subarrays required to form a given Array | Function to find the minimum number of operations required to change all the array of zeros such that every element is greater than the given array ; Declaring the difference array of size N ; Number of operations ; First update the D [ i ] value with the previous value ; The index i has to be incremented ; We have to perform ( b [ i ] - d [ i ] ) operations more ; Increment the range i to i + k by need ; Check if i + k is valid index ; Driver Code ; Function Call
def find_minimum_operations ( n , b , k ) : NEW_LINE INDENT d = [ 0 for i in range ( n + 1 ) ] NEW_LINE operations = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT d [ i ] += d [ i - 1 ] NEW_LINE if b [ i ] > d [ i ] : NEW_LINE INDENT operations += ( b [ i ] - d [ i ] ) NEW_LINE need = ( b [ i ] - d [ i ] ) NEW_LINE d [ i ] += need NEW_LINE if i + k <= n : NEW_LINE INDENT d [ i + k ] -= need NEW_LINE DEDENT DEDENT DEDENT return operations NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE b = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE k = 2 NEW_LINE print ( find_minimum_operations ( n , b , k ) ) NEW_LINE DEDENT