Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
Last Updated :
07 Jun, 2022
Given two integers X and Y. X and Y represent any two values among (A + B), (A + C) and (B + C). The task is to find A, B and C such that A + B + C is minimum possible.
Examples:
Input: X = 3, Y = 4
Output: 2 1 2
A = 2, B = 1, C = 2.
Then A + B = 3 and A + C = 4.
A + B + C = 5 which is minimum possible.
Input: X = 123, Y = 13
Output: 1 12 111
Approach: Let X = A + B and Y = B + C. If X > Y let’s swap them. Note that A + B + C = A + B + (Y – B) = A + Y. That’s why it’s optimal to minimize the value of A. So the value of A can always be 1. Then B = X – A and C = Y – B.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MinimumValue( int x, int y)
{
if (x > y)
swap(x, y);
int a = 1;
int b = x - 1;
int c = y - b;
cout << a << " " << b << " " << c;
}
int main()
{
int x = 123, y = 13;
MinimumValue(x, y);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void MinimumValue( int x, int y)
{
if (x > y)
{
int temp = x;
x = y;
y = temp;
}
int a = 1 ;
int b = x - 1 ;
int c = y - b;
System.out.print( a + " " + b + " " + c);
}
public static void main (String[] args)
{
int x = 123 , y = 13 ;
MinimumValue(x, y);
}
}
|
Python3
def MinimumValue(x, y):
if (x > y):
x, y = y, x
a = 1
b = x - 1
c = y - b
print (a, b, c)
x = 123
y = 13
MinimumValue(x, y)
|
C#
using System;
class GFG
{
static void MinimumValue( int x, int y)
{
if (x > y)
{
int temp = x;
x = y;
y = temp;
}
int a = 1;
int b = x - 1;
int c = y - b;
Console.WriteLine( a + " " + b + " " + c);
}
public static void Main ()
{
int x = 123, y = 13;
MinimumValue(x, y);
}
}
|
Javascript
<script>
function MinimumValue(x, y)
{
if (x > y)
{
var temp = x;
x = y;
y = temp;
}
var a = 1;
var b = x - 1;
var c = y - b;
document.write( a + " " + b + " " + c);
}
var x = 123, y = 13;
MinimumValue(x, y);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...