Making three numbers equal with the given operations
Last Updated :
21 Jun, 2022
Given four positive integers A, B, C, and K. The task is to check if it is possible to equalize the three integers A, B, and C with the help of K and make K equal to 0. In one operation, you can subtract any value from K (if it remains greater than equal to 0 after subtraction) and add the new value to any of the three integers A, B, or C.
Examples:
Input: A = 6, B = 3, C = 2, K = 7
Output: Yes
Operation 1: Add 3 to B and subtract 3 from K.
A = 6, B = 6, C = 2 and K = 4
Operation 2: Add 4 to C and subtract 4 from K.
A = 6, B = 6, C = 6 and K = 0
Input: A = 10, B = 20, C = 17, K = 15
Output: No
Approach: Check whether it is possible to equalize the three numbers by sorting the three numbers and subtracting the value of K by the sum of the difference of 3rd and 2nd element and the 3rd and 1st element. If K is still greater than 0 and can be divided among the three elements equally then only the three elements can be made equal and K can be made equal to 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canBeEqual( int a, int b, int c, int k)
{
int arr[3];
arr[0] = a;
arr[1] = b;
arr[2] = c;
sort(arr, arr + 3);
int diff = 2 * arr[2] - arr[1] - arr[0];
k = k - diff;
if (k < 0 || k % 3 != 0)
return false ;
return true ;
}
int main()
{
int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
if (canBeEqual(a1, b1, c1, k1))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean canBeEqual( int a, int b, int c, int k)
{
int []arr = new int [ 3 ];
arr[ 0 ] = a;
arr[ 1 ] = b;
arr[ 2 ] = c;
Arrays.sort(arr);
int diff = 2 * arr[ 2 ] - arr[ 1 ] - arr[ 0 ];
k = k - diff;
if (k < 0 || k % 3 != 0 )
return false ;
return true ;
}
public static void main(String[] args)
{
int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ;
if (canBeEqual(a1, b1, c1, k1))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def canBeEqual(a, b, c, k) :
arr = [ 0 ] * 3 ;
arr[ 0 ] = a;
arr[ 1 ] = b;
arr[ 2 ] = c;
arr.sort()
diff = 2 * arr[ 2 ] - arr[ 1 ] - arr[ 0 ];
k = k - diff;
if (k < 0 or k % 3 ! = 0 ) :
return False ;
return True ;
if __name__ = = "__main__" :
a1 = 6 ; b1 = 3 ; c1 = 2 ; k1 = 7 ;
if (canBeEqual(a1, b1, c1, k1)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool canBeEqual( int a, int b, int c, int k)
{
int []arr = new int [3];
arr[0] = a;
arr[1] = b;
arr[2] = c;
Array.Sort(arr);
int diff = 2 * arr[2] - arr[1] - arr[0];
k = k - diff;
if (k < 0 || k % 3 != 0)
return false ;
return true ;
}
public static void Main(String[] args)
{
int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
if (canBeEqual(a1, b1, c1, k1))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function canBeEqual(a, b, c, k)
{
var arr = Array(3);
arr[0] = a;
arr[1] = b;
arr[2] = c;
arr.sort((a,b)=> a-b);
var diff = 2 * arr[2] - arr[1] - arr[0];
k = k - diff;
if (k < 0 || k % 3 != 0)
return false ;
return true ;
}
var a1 = 6, b1 = 3, c1 = 2, k1 = 7;
if (canBeEqual(a1, b1, c1, k1))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...