# Making three numbers equal with the given operations

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 form 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// 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; ` `    ``arr = a; ` `    ``arr = b; ` `    ``arr = 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 - arr - arr; ` ` `  `    ``// 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``; ` `    ``arr = a; ` `    ``arr = b; ` `    ``arr = 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 - arr - arr; ` ` `  `    ``// 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 `

Output:

```Yes
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.