Skip to content
Related Articles

Related Articles

Making three numbers equal with the given operations
  • Last Updated : 04 Feb, 2020

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

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 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
Output:
Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :