Make A, B and C equal by adding total value N to them
Given 3 integers A, B, and C, and an integer N, the task is to distribute N among all the other 3 numbers such that at the end A = B = C. If the distribution is possible then print “Yes” otherwise output “No”.
Examples:
Input: A = 5, B = 3, C = 2, N = 8
Output: Yes
Explanation:
We can distribute N = 8 by adding 1 to A, 3 to B and 4 to C to get all of them as 6. Hence the distribution is possible.
Input: A = 10, B = 20, C = 15, N = 14
Output: No
Explanation:
Distribution of N among all three integers to make them equal is not possible.
Approach:
To solve the problem mentioned above we have to follow the steps given below:
- Find maximum out of all the three integers A, B and C. Let that be integer K
- Multiply the integer K by 3 and then subtract it by the sum of the three integers.
- Check if the difference of that number and N is divisible by 3 or not.
- If it is, then the output is “Yes”, otherwise it is not possible to distribute the number.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void distributeN( int A, int B, int C, int n)
{
int max1 = max(A, B);
int max2 = max(B, C);
int maximum = max(max1, max2);
int sum = A + B + C;
int p = (3 * maximum) - sum;
int diff = n - p;
if (diff < 0 || diff % 3)
cout << "No" ;
else
cout << "Yes" ;
}
int main()
{
int A = 10, B = 20;
int C = 15, n = 14;
distributeN(A, B, C, n);
return 0;
}
|
Java
class GFG{
static void distributeN( int A, int B, int C,
int n)
{
int max1 = Math.max(A, B);
int max2 = Math.max(B, C);
int maximum = Math.max(max1, max2);
int sum = A + B + C;
int p = ( 3 * maximum) - sum;
int diff = n - p;
if (diff < 0 || diff % 3 == 0 )
System.out.print( "No" );
else
System.out.print( "Yes" );
}
public static void main(String[] args)
{
int A = 10 , B = 20 ;
int C = 15 , n = 14 ;
distributeN(A, B, C, n);
}
}
|
Python3
def distributeN(A, B, C, n):
maximum = max (A, B, C)
sum = A + B + C
p = ( 3 * maximum) - sum
diff = n - p
if diff < 0 or diff % 3 :
print "No"
else :
print "Yes"
A = 10
B = 20
C = 15
n = 14
distributeN(A, B, C, n)
|
C#
using System;
class GFG{
static void distributeN( int A, int B, int C,
int n)
{
int max1 = Math.Max(A, B);
int max2 = Math.Max(B, C);
int maximum = Math.Max(max1, max2);
int sum = A + B + C;
int p = (3 * maximum) - sum;
int diff = n - p;
if (diff < 0 || diff % 3 == 0)
Console.Write( "No" );
else
Console.Write( "Yes" );
}
public static void Main(String[] args)
{
int A = 10, B = 20;
int C = 15, n = 14;
distributeN(A, B, C, n);
}
}
|
Javascript
<script>
function distributeN(A, B, C, n)
{
let max1 = Math.max(A, B);
let max2 = Math.max(B, C);
let maximum = Math.max(max1, max2);
let sum = A + B + C;
let p = (3 * maximum) - sum;
let diff = n - p;
if (diff < 0 || diff % 3)
document.write( "No" );
else
document.write( "Yes" );
}
let A = 10, B = 20;
let C = 15, n = 14;
distributeN(A, B, C, n);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
18 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...