Minimize value of |A – X| + |B – Y| + |C – Z| such that X * Y = Z
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++
#include <bits/stdc++.h>
using namespace std;
int minimizeCost( int A, int B, int C)
{
int ans = A + B + C;
for ( int i = 1; i <= 2 * C; i++) {
int j = 0;
while (i * j <= 2 * C) {
ans = min(ans, abs (A - i) + abs (B - j)
+ abs (i * j - C));
j++;
}
}
return ans;
}
int main()
{
int A = 19, B = 28, C = 522;
cout << minimizeCost(A, B, C);
return 0;
}
|
Java
class GFG{
public static int minimizeCost( int A, int B, int C)
{
int ans = A + B + C;
for ( int i = 1 ; i <= 2 * C; i++) {
int j = 0 ;
while (i * j <= 2 * C) {
ans = Math.min(ans, Math.abs(A - i) + Math.abs(B - j)
+ Math.abs(i * j - C));
j++;
}
}
return ans;
}
public static void main(String args[])
{
int A = 19 , B = 28 , C = 522 ;
System.out.print(minimizeCost(A, B, C));
}
}
|
Python3
def minimizeCost(A, B, C):
ans = A + B + C
for i in range ( 1 , 2 * C + 1 ):
j = 0
while (i * j < = 2 * C):
ans = min (ans, abs (A - i) + abs (B - j) + abs (i * j - C))
j + = 1
return ans
A = 19
B = 28
C = 522
print (minimizeCost(A, B, C))
|
C#
using System;
class GFG{
public static int minimizeCost( int A, int B, int C)
{
int ans = A + B + C;
for ( int i = 1; i <= 2 * C; i++) {
int j = 0;
while (i * j <= 2 * C) {
ans = Math.Min(ans, Math.Abs(A - i) + Math.Abs(B - j)
+ Math.Abs(i * j - C));
j++;
}
}
return ans;
}
public static void Main(String []args)
{
int A = 19, B = 28, C = 522;
Console.Write(minimizeCost(A, B, C));
}
}
|
Javascript
<script>
function minimizeCost(A, B, C)
{
let ans = A + B + C;
for (let i = 1; i <= 2 * C; i++) {
let j = 0;
while (i * j <= 2 * C) {
ans = Math.min(ans, Math.abs(A - i) + Math.abs(B - j)
+ Math.abs(i * j - C));
j++;
}
}
return ans;
}
let A = 19, B = 28, C = 522;
document.write(minimizeCost(A, B, C));
</script>
|
Time Complexity: O(C*log C)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...