给定四个正整数A , B , C和K。任务是检查是否有可能借助K使三个整数A , B和C相等,并使K等于0 。在一个操作中,您可以从K中减去任何值(如果在减去后仍保持大于0的值),并将新值添加到三个整数A , B或C中的任何一个。
例子:
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
的方法:检查是否有可能通过排序三个数字,并通过第3次和第2次元件和3次和1个元素的差的总和中减去K的值以均衡三个数字。如果K仍大于0且可以在三个元素之间平均分配,则只有三个元素可以相等,而K可以等于0 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true if a, b and c can
// be made equal with the given operations
bool canBeEqual(int a, int b, int c, int k)
{
int arr[3];
arr[0] = a;
arr[1] = b;
arr[2] = c;
// Sort the three numbers
sort(arr, arr + 3);
// Find the sum of difference of the 3rd and
// 2nd element and the 3rd and 1st element
int diff = 2 * arr[2] - arr[1] - arr[0];
// Subtract the difference from k
k = k - diff;
// Check the required condition
if (k < 0 || k % 3 != 0)
return false;
return true;
}
// Driver code
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
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function that returns true if a, b and c can
// be made equal with the given operations
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;
// Sort the three numbers
Arrays.sort(arr);
// Find the sum of difference of the 3rd and
// 2nd element and the 3rd and 1st element
int diff = 2 * arr[2] - arr[1] - arr[0];
// Subtract the difference from k
k = k - diff;
// Check the required condition
if (k < 0 || k % 3 != 0)
return false;
return true;
}
// Driver code
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");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
# Function that returns true if a, b and c can
# be made equal with the given operations
def canBeEqual(a, b, c, k) :
arr = [0] * 3;
arr[0] = a;
arr[1] = b;
arr[2] = c;
# Sort the three numbers
arr.sort()
# Find the sum of difference of the 3rd and
# 2nd element and the 3rd and 1st element
diff = 2 * arr[2] - arr[1] - arr[0];
# Subtract the difference from k
k = k - diff;
# Check the required condition
if (k < 0 or k % 3 != 0) :
return False;
return True;
# Driver code
if __name__ == "__main__" :
a1 = 6; b1 = 3; c1 = 2; k1 = 7;
if (canBeEqual(a1, b1, c1, k1)) :
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that returns true if a, b and c can
// be made equal with the given operations
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;
// Sort the three numbers
Array.Sort(arr);
// Find the sum of difference of the 3rd and
// 2nd element and the 3rd and 1st element
int diff = 2 * arr[2] - arr[1] - arr[0];
// Subtract the difference from k
k = k - diff;
// Check the required condition
if (k < 0 || k % 3 != 0)
return false;
return true;
}
// Driver code
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");
}
}
// This code is contributed by 29AjayKumar
输出:
Yes