Make the triplet same by adding 2, 4, and 6 to the given numbers
Last Updated :
07 Aug, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int equalTriplet( int x1, int x2, int x3)
{
int v = x1 + x2 + x3;
if (v % 3) {
return -1;
}
if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0
|| x1 % 2 != v / 3 % 2)
return -1;
v /= 3;
return ( abs (x1 - v) + abs (x2 - v) + abs (x3 - v)) / 4;
}
int main()
{
int x = 10, y = 100, z = 1000;
int ans = equalTriplet(x, y, z);
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;
if (v % 3 != 0 ) {
return - 1 ;
}
if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2
|| v % 3 != 0 || x1 % 2 != v / 3 % 2 ) {
return - 1 ;
}
v /= 3 ;
return (Math.abs(x1 - v) + Math.abs(x2 - v)
+ Math.abs(x3 - v))
/ 4 ;
}
public static void main(String[] args)
{
int x = 10 , y = 100 , z = 1000 ;
int ans = equalTriplet(x, y, z);
System.out.println(ans);
}
}
|
Python3
def equalTriplet(x1, x2, x3):
v = x1 + x2 + x3
if v % 3 :
return - 1
if x1 % 2 ! = x2 % 2 or x2 % 2 ! = x3 % 2 or v % 3 ! = 0 or x1 % 2 ! = v / / 3 % 2 :
return - 1
v / / = 3
return ( abs (x1 - v) + abs (x2 - v) + abs (x3 - v)) / / 4
x, y, z = 10 , 100 , 1000
ans = equalTriplet(x, y, z)
print (ans)
|
C#
using System;
public class GFG
{
public static int EqualTriplet( int x1, int x2, int x3)
{
int v = x1 + x2 + x3;
if (v % 3 != 0)
{
return -1;
}
if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0 || x1 % 2 != v / 3 % 2)
{
return -1;
}
v /= 3;
return (Math.Abs(x1 - v) + Math.Abs(x2 - v) + Math.Abs(x3 - v)) / 4;
}
public static void Main( string [] args)
{
int x = 10, y = 100, z = 1000;
int ans = EqualTriplet(x, y, z);
Console.WriteLine(ans);
}
}
|
Javascript
function equalTriplet (x1, x2, x3)
{
let v = x1 + x2 + x3;
if (v % 3) {
return -1;
}
if (x1 % 2 != x2 % 2 || x2 % 2 != x3 % 2 || v % 3 != 0
|| x1 % 2 != v / 3 % 2)
return -1;
v /= 3;
return Math.floor(Math.abs(x1 - v) + Math.abs(x2 - v) + Math.abs(x3 - v)) / 4;
}
let x = 10, y = 100, z = 1000;
let ans = equalTriplet(x, y, z);
console.log(ans);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...