Minimize value of | A | Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code
def minimizeCost ( A , B , C ) : NEW_LINE INDENT ans = A + B + C NEW_LINE for i in range ( 1 , 2 * C + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( i * j <= 2 * C ) : NEW_LINE INDENT ans = min ( ans , abs ( A - i ) + abs ( B - j ) + abs ( i * j - C ) ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = 19 NEW_LINE B = 28 NEW_LINE C = 522 NEW_LINE print ( minimizeCost ( A , B , C ) ) NEW_LINE