# Minimize value of |A – X| + |B – Y| + |C – Z| such that X * Y = Z

• Difficulty Level : Medium
• Last Updated : 29 Sep, 2021

Given three integers A, B, and C, the task is to find the minimum possible value of |A – X| + |B – Y| + |C – Z| such that X * Y = Z.

Example:

Input: A = 19, B = 28, C = 522
Output: 2
Explanation: The most optimal choice of X, Y, and Z for the given A, B, and C are X = 18, Y = 29, and Z = 522. The equation X * Y = Z holds true and the value of |A – X| + |B – Y| + |C – Z| = 2 which is minimum possible.

Input: A = 11, B = 11, C = 121
Output: 0
Explanation: The given values of A, B, and C satisfies A * B = C. Therefore the most optimal choice is X = A, Y = B, and Z = C.

Approach: The above problem can be solved using the following observations:

• The maximum value of |A – X| + |B – Y| + |C – Z| can be A + B + C for X, Y, and Z equal to 0.
• Based on the above observation, iterating over all the values of i * j  such that i * j <= 2 * C  and choosing the best value is the optimal choice.

Therefore, iterate over all values of i in the range [1, 2*C], and for every i, iterate over all values of j such that i * j <= 2 * C and keep track of the minimum possible value of |A – i| + |B – j| + |C –  i * j|.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// 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` `int` `minimizeCost(``int` `A, ``int` `B, ``int` `C)` `{` `    ``// Stores the minimum value of` `    ``// |A - X| + |B - Y| + |C - Z|` `    ``// such that X * Y = Z` `    ``int` `ans = A + B + C;`   `    ``// Iterate over all values of i` `    ``// in the range [1, 2*C]` `    ``for` `(``int` `i = 1; i <= 2 * C; i++) {` `        ``int` `j = 0;`   `        ``// Iterate over all values of` `        ``// j such that i*j <= 2*c` `        ``while` `(i * j <= 2 * C) {`   `            ``// Update the value of ans` `            ``ans = min(ans, ``abs``(A - i) + ``abs``(B - j)` `                               ``+ ``abs``(i * j - C));` `            ``j++;` `        ``}` `    ``}`   `    ``// Return answer` `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `A = 19, B = 28, C = 522;` `    ``cout << minimizeCost(A, B, C);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach`   `class` `GFG{`   `// 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` `public` `static` `int` `minimizeCost(``int` `A, ``int` `B, ``int` `C)` `{` `    ``// Stores the minimum value of` `    ``// |A - X| + |B - Y| + |C - Z|` `    ``// such that X * Y = Z` `    ``int` `ans = A + B + C;`   `    ``// Iterate over all values of i` `    ``// in the range [1, 2*C]` `    ``for` `(``int` `i = ``1``; i <= ``2` `* C; i++) {` `        ``int` `j = ``0``;`   `        ``// Iterate over all values of` `        ``// j such that i*j <= 2*c` `        ``while` `(i * j <= ``2` `* C) {`   `            ``// Update the value of ans` `            ``ans = Math.min(ans, Math.abs(A - i) + Math.abs(B - j)` `                               ``+ Math.abs(i * j - C));` `            ``j++;` `        ``}` `    ``}`   `    ``// Return answer` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `main(String args[])` `{` `    ``int` `A = ``19``, B = ``28``, C = ``522``;` `    ``System.out.print(minimizeCost(A, B, C));`   `}`   `}`   `// This code is contributed by gfgking.`

## Python3

 `# Python Program to implement` `# the above approach`   `# 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` `def` `minimizeCost(A, B, C):`   `    ``# Stores the minimum value of` `    ``# |A - X| + |B - Y| + |C - Z|` `    ``# such that X * Y = Z` `    ``ans ``=` `A ``+` `B ``+` `C`   `    ``# Iterate over all values of i` `    ``# in the range [1, 2*C]` `    ``for` `i ``in` `range``(``1``, ``2` `*` `C ``+` `1``):` `        ``j ``=` `0`   `        ``# Iterate over all values of` `        ``# j such that i*j <= 2*c` `        ``while` `(i ``*` `j <``=` `2` `*` `C):`   `            ``# Update the value of ans` `            ``ans ``=` `min``(ans, ``abs``(A ``-` `i) ``+` `abs``(B ``-` `j) ``+` `abs``(i ``*` `j ``-` `C))` `            ``j ``+``=` `1` `    `    `    ``# Return answer` `    ``return` `ans`     `# Driver Code` `A ``=` `19` `B ``=` `28` `C ``=` `522` `print``(minimizeCost(A, B, C))`   `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# program for the above approach` `using` `System;` `class` `GFG{`   `// 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` `public` `static` `int` `minimizeCost(``int` `A, ``int` `B, ``int` `C)` `{` `  `  `    ``// Stores the minimum value of` `    ``// |A - X| + |B - Y| + |C - Z|` `    ``// such that X * Y = Z` `    ``int` `ans = A + B + C;`   `    ``// Iterate over all values of i` `    ``// in the range [1, 2*C]` `    ``for` `(``int` `i = 1; i <= 2 * C; i++) {` `        ``int` `j = 0;`   `        ``// Iterate over all values of` `        ``// j such that i*j <= 2*c` `        ``while` `(i * j <= 2 * C) {`   `            ``// Update the value of ans` `            ``ans = Math.Min(ans, Math.Abs(A - i) + Math.Abs(B - j)` `                               ``+ Math.Abs(i * j - C));` `            ``j++;` `        ``}` `    ``}`   `    ``// Return answer` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `Main(String []args)` `{` `    ``int` `A = 19, B = 28, C = 522;` `    ``Console.Write(minimizeCost(A, B, C));` `}` `}`   `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output:

`2`

Time Complexity: O(C*log C)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :