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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function that returns true if a, b and c can// be made equal with the given operationsbool 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 codeint 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 approachimport java.util.*;Â
class GFG{Â
// Function that returns true if a, b and c can// be made equal with the given operationsstatic 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 codepublic 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 approachusing System;Â
class GFG{Â
// Function that returns true if a, b and c can// be made equal with the given operationsstatic 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 codepublic 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 |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function that returns true if a, b and c can// be made equal with the given operationsfunction canBeEqual(a, b, c, k){Â Â Â Â var arr = Array(3);Â Â Â Â arr[0] = a;Â Â Â Â arr[1] = b;Â Â Â Â arr[2] = c;Â
    // Sort the three numbers    arr.sort((a,b)=> a-b);Â
    // Find the sum of difference of the 3rd and    // 2nd element and the 3rd and 1st element    var 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 codevar a1 = 6, b1 = 3, c1 = 2, k1 = 7;if (canBeEqual(a1, b1, c1, k1))    document.write( "Yes");else    document.write( "No");Â
// This code is contributed by rrrtnx.</script> |
Yes
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
