Open In App

Make the triplet same by adding 2, 4, and 6 to the given numbers

Last Updated : 07 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given three integers x1, x2, and x3, we can perform an operation that simultaneously adds 2 to one of them, adds 4 to another, and adds 6 to the third. We can perform this operation any number of times on any permutation of the indices (1, 2, 3), the task is to determine whether it is possible to make x1 = x2 = x3 through some sequence of these operations. If it is possible, find the minimum number of times we need to perform the operation to achieve this goal. If not possible print -1.

Examples:

Input: x1 = 2, x2 = 8, x3 = 8
Output: 2
Explanation: Initially (x1=2, x2=8, x3=8). 

  • 1st step : (x1+6, x2+4, x3+2) makes (8 12 10). 
  • 2nd Step:  (x1+6, x2+2, x3+4) makes (14 14 14), thus in two steps we can make triplet equal.

Input: x1 = 5, x2 = 10, x3 = 10
Output: -1

Approach: Since the differences between the values of x are the only thing that matters in this problem, we can assume that the first value is zero without loss of generality. Then, the problem reduces to finding a way to express the other two values as a linear combination of -2, 0, and 2 instead of +2, +4, and +6.

Observations:

  • The algorithm presented the fact that the sum of three integers is a multiple of 3 if and only if the integers have the same remainder when divided by 3.
  • The solution shows that a necessary condition for the three numbers to be transformed into three equal numbers using the given operations is that the sum of the three numbers is a multiple of 3. Then, it uses a counting argument to prove that this condition is also sufficient.
  • The algorithm calculates the distance of each number from the value that makes their sum a multiple of 3. It shows that each operation can decrease this distance by at most 4, and if the distance is not zero, it can always be decreased by 4 using the given operations. Therefore, the minimum number of operations required to transform the three numbers into three equal numbers is equal to four times the distance of the three numbers from the value that makes their sum a multiple of 3.
  • Finally, the algorithm shows that the problem is invariant under adding a constant number to each of the three given numbers, and chooses a specific set of operations to make the problem easier to solve.

Below are the steps for the above approach:

  • Initialize a variable say v, to store the sum of the triplet.
  • Check if the sum of the triplet is not divisible by 3, it is not possible to make the triplet equal by a given operation.
  • Check if the parity of the triplet and the sum/3 are not the same then also it is not possible to make the triplet equal,
    • If (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0 || x1 % 2 != v / 3 % 2), return -1.
  • Find the sum of the modulus difference of the sum/3 and the triplet individually. It is the difference target we have to cover by the operation.
  • In one operation we can decrease the difference by at most 4, so divide it by 4 to get the minimum number of operations.
    • abs(x1 – v) + abs(x2 – v) + abs(x3 – v)) / 4

Below is the code for the above approach:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
int equalTriplet(int x1, int x2, int x3)
{
 
    int v = x1 + x2 + x3;
 
    // Sum of triplet
    if (v % 3) {
 
        // Cheking is it divisible by 3
        // or not, if not return -1
        return -1;
    }
 
    // Cheking all the element x1, x2, x3
    // and sum/3 is in same parity or not
    if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0
        || x1 % 2 != v / 3 % 2)
        return -1;
    v /= 3;
 
    // Returning the sum of abs
    // differnce / 4
    return (abs(x1 - v) + abs(x2 - v) + abs(x3 - v)) / 4;
}
 
// Drivers code
int main()
{
    int x = 10, y = 100, z = 1000;
    int ans = equalTriplet(x, y, z);
 
    // Function Call
    cout << ans << endl;
}


Java




import java.util.*;
 
public class Main {
    public static int equalTriplet(int x1, int x2, int x3)
    {
        int v = x1 + x2 + x3;
 
        // Sum of triplet
        if (v % 3 != 0) {
            // Cheking is it divisible by 3
            // or not, if not return -1
            return -1;
        }
 
        // Cheking all the element x1, x2, x3
        // and sum/3 is in same parity or not
        if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2
            || v % 3 != 0 || x1 % 2 != v / 3 % 2) {
            return -1;
        }
 
        v /= 3;
 
        // Returning the sum of abs
        // differnce / 4
        return (Math.abs(x1 - v) + Math.abs(x2 - v)
                + Math.abs(x3 - v))
            / 4;
    }
 
    // Drivers code
    public static void main(String[] args)
    {
        int x = 10, y = 100, z = 1000;
        int ans = equalTriplet(x, y, z);
 
        // Function Call
        System.out.println(ans);
    }
}


Python3




def equalTriplet(x1, x2, x3):
    v = x1 + x2 + x3
 
    # Sum of triplet
    if v % 3:
        # Checking if it is divisible by 3 or not, if not return -1
        return -1
 
    # Checking all the element x1, x2, x3
    # and sum/3 is in the same parity or not
    if x1 % 2 != x2 % 2 or x2 % 2 != x3 % 2 or v % 3 != 0 or x1 % 2 != v // 3 % 2:
        return -1
    v //= 3
 
    # Returning the sum of abs difference / 4
    return (abs(x1 - v) + abs(x2 - v) + abs(x3 - v)) // 4
 
# Drivers code
x, y, z = 10, 100, 1000
ans = equalTriplet(x, y, z)
 
# Function Call
print(ans)


C#




// C# code for the above approach:
 
using System;
 
public class GFG
{
    public static int EqualTriplet(int x1, int x2, int x3)
    {
        int v = x1 + x2 + x3;
 
        // Check if the sum of triplet is divisible by 3
        if (v % 3 != 0)
        {
            // If not divisible by 3, return -1
            return -1;
        }
 
        // Check if all elements x1, x2, x3 have the same parity as the sum/3
        if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0 || x1 % 2 != v / 3 % 2)
        {
            // If the parities are not the same, return -1
            return -1;
        }
 
        v /= 3;
 
        // Returning the sum of absolute differences / 4
        return (Math.Abs(x1 - v) + Math.Abs(x2 - v) + Math.Abs(x3 - v)) / 4;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int x = 10, y = 100, z = 1000;
        int ans = EqualTriplet(x, y, z);
 
        // Function Call
        Console.WriteLine(ans);
    }
}


Javascript




// JS code for the above approach:
 
function equalTriplet (x1, x2, x3)
{
 
    let v = x1 + x2 + x3;
 
    // Sum of triplet
    if (v % 3) {
 
        // Cheking is it divisible by 3
        // or not, if not return -1
        return -1;
    }
 
    // Cheking all the element x1, x2, x3
    // and sum/3 is in same parity or not
    if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0
        || x1 % 2 != v / 3 % 2)
        return -1;
    v /= 3;
 
    // Returning the sum of abs
    // differnce / 4
    return Math.floor(Math.abs(x1 - v) + Math.abs(x2 - v) + Math.abs(x3 - v)) / 4;
}
 
// Drivers code
let x = 10, y = 100, z = 1000;
let ans = equalTriplet(x, y, z);
 
// Function Call
console.log(ans);


Output

315

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads