Given three integers A, B, C denoting a triplet and three integers P, Q, R denoting another triplet. Repeatedly select any integer and add or multiply it to all elements in a subset (A, B, C) with that integer until the two given triplets become equal. The task is to find the minimum number of such operations required to make the two triplets equal.
Example:
Input: (A, B, C) = (3, 4, 5), (P, Q, R) = (6, 3, 10) Output: 2 Explanation: Step 1: Multiply 2 with all elements of the subset {3, 5}. Triplet (A, B, C) becomes (6, 4, 10). Step 2: Add -1 to the subset {4}. Triplet (A, B, C) becomes (6, 3, 10) which is same as the triplet (P, Q, R). Therefore, the minimum number of operations required is 2.
Input: (A, B, C) = (7, 6, 8), (p, q, r) = (2, 2, 2) Output: 2 Explanation: Step 1: Multiply all elements of the subset (7, 6, 8) with 0. (A, B, C) modifies to (0, 0, 0). Step 2: Add 2 to all elements of the subset (0, 0, 0). (A, B, C) modifies to (2, 2, 2) same as the triplet (P, Q, R). Therefore, the minimum number of operations required is 2.
Approach: Follow the steps below to solve the given problem:
- In each step, either add or multiply an integer to a subset of the triplet. The integer can be chosen as:
- In Addition: In each step, try to fix at least one of the numbers. Hence, the set of all possible numbers that needs to be tried to add is restricted to (B[i] – A[i]) for at least one i.
- In Multiplication: Following the same logic, multiply m to a subset such that for at least one i, A[i]*m = B[i] is satisfied.
- So far, all the operations had the underlying assumption that after the operation, at least one of the values of A[i] is converted to B[i].
Consider the following Case: A[] = [2, 3, 4] and B[] = [-20, – 1, 18] The above transformation can be done in just two steps by multiplying all numbers by 19 and then adding -58 to each number. Such cases have to be taken care of separately.
- Therefore, use recursion to solve the problem which takes care of all the edge cases and will give us the minimum numbers of operations to be performed for the conversion.
- Print the minimum count of steps after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool equal( int a[], int b[])
{
for ( int i = 0; i < 3; i++) {
if (a[i] != b[i]) {
return false ;
}
}
return true ;
}
int mulFac( int a, int b, int c, int d)
{
if (b != a and (d - c) % (b - a) == 0) {
return (d - c) / (b - a);
}
else {
return 1;
}
}
void getMinOperations( int a[], int b[],
int & ans, int num = 0)
{
if (num >= ans)
return ;
if (equal(a, b)) {
ans = min(ans, num);
return ;
}
if (num >= 2)
return ;
set< int > add;
add.insert(b[0] - a[0]);
add.insert(b[1] - a[1]);
add.insert(b[2] - a[2]);
set< int > mult;
for ( int i = 0; i < 3; i++) {
if (a[i] != 0
&& b[i] % a[i] == 0) {
mult.insert(b[i] / a[i]);
}
}
mult.insert(mulFac(a[0], a[1],
b[0], b[1]));
mult.insert(mulFac(a[2], a[1],
b[2], b[1]));
mult.insert(mulFac(a[0], a[2],
b[0], b[2]));
mult.insert(0);
for ( int mask = 1; mask <= 7; mask++) {
vector< int > subset;
for ( int j = 0; j < 3; j++)
if (mask & (1 << j))
subset.push_back(j);
for ( auto x : add) {
int temp[3];
for ( int j = 0; j < 3; j++)
temp[j] = a[j];
for ( auto e : subset)
temp[e] += x;
getMinOperations(temp, b,
ans, num + 1);
}
for ( auto x : mult) {
int temp[3];
for ( int j = 0; j < 3; j++)
temp[j] = a[j];
for ( auto e : subset)
temp[e] *= x;
getMinOperations(temp, b,
ans, num + 1);
}
}
}
int main()
{
int a[] = { 4, 5, 6 };
int b[] = { 0, 1, 0 };
int ans = 3;
getMinOperations(a, b, ans);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int ans_max = 0 ;
static boolean equal( int [] a, int [] b)
{
for ( int i = 0 ; i < 3 ; i++)
{
if (a[i] != b[i])
{
return false ;
}
}
return true ;
}
static int mulFac( int a, int b,
int c, int d)
{
if (b != a && (d - c) % (b - a) == 0 )
{
return (d - c) / (b - a);
}
else
{
return 1 ;
}
}
static void getMinOperations( int [] a, int [] b,
int ans, int num)
{
if (num >= ans)
{
return ;
}
if (equal(a, b))
{
ans = Math.min(ans, num);
ans_max = ans;
return ;
}
if (num >= 2 )
{
return ;
}
Set<Integer> ad = new HashSet<Integer>();
ad.add(b[ 0 ] - a[ 0 ]);
ad.add(b[ 1 ] - a[ 1 ]);
ad.add(b[ 2 ] - a[ 2 ]);
Set<Integer> mult = new HashSet<Integer>();
for ( int i = 0 ; i < 3 ; i++)
{
if (a[i] != 0 && b[i] % a[i] == 0 )
{
mult.add(b[i] / a[i]);
}
}
mult.add(mulFac(a[ 0 ], a[ 1 ],
b[ 0 ], b[ 1 ]));
mult.add(mulFac(a[ 2 ], a[ 1 ],
b[ 2 ], b[ 1 ]));
mult.add(mulFac(a[ 0 ], a[ 2 ],
b[ 0 ], b[ 2 ]));
mult.add( 0 );
for ( int mask = 1 ; mask <= 7 ; mask++)
{
Vector<Integer> subset = new Vector<Integer>();
for ( int j = 0 ; j < 3 ; j++)
{
if ((mask & ( 1 << j)) != 0 )
{
subset.add(j);
}
}
for ( int x : ad)
{
int [] temp = new int [ 3 ];
for ( int j = 0 ; j < 3 ; j++)
{
temp[j] = a[j];
}
for ( int e:subset)
{
temp[e] += x;
}
getMinOperations(temp, b, ans,
num + 1 );
}
for ( int x:mult)
{
int [] temp = new int [ 3 ];
for ( int j = 0 ; j < 3 ; j++)
{
temp[j] = a[j];
}
for ( int e:subset)
{
temp[e] *= x;
}
getMinOperations(temp, b,
ans, num + 1 );
}
}
}
public static void main(String[] args)
{
int [] a = { 4 , 5 , 6 };
int [] b = { 0 , 1 , 0 };
int ans = 3 ;
getMinOperations(a, b, ans, 0 );
System.out.println(ans_max);
}
}
|
Python3
ans_max = 0
def equal(a, b):
for i in range ( 3 ):
if (a[i] ! = b[i]):
return False
return True
def mulFac(a, b, c, d):
if (b ! = a and (d - c) % (b - a) = = 0 ):
return (d - c) / / (b - a)
else :
return 1
def getMinOperations(a, b, ans, num):
global ans_max
if (num > = ans):
return 0
if (equal(a, b)):
ans = min (ans, num)
ans_max = ans
return ans
if (num > = 2 ):
return 0
add = {}
add[(b[ 0 ] - a[ 0 ])] = 1
add[(b[ 1 ] - a[ 1 ])] = 1
add[(b[ 2 ] - a[ 2 ])] = 1
mult = {}
for i in range ( 3 ):
if (a[i] ! = 0 and b[i] % a[i] = = 0 ):
mult[b[i] / / a[i]] = 1
mult[mulFac(a[ 0 ], a[ 1 ], b[ 0 ], b[ 1 ])] = 1
mult[mulFac(a[ 2 ], a[ 1 ], b[ 2 ], b[ 1 ])] = 1
mult[mulFac(a[ 0 ], a[ 2 ], b[ 0 ], b[ 2 ])] = 1
mult[ 0 ] = 1
for mask in range ( 1 , 8 ):
subset = {}
for j in range ( 3 ):
if (mask & ( 1 << j)):
subset[j] = 1
for x in add:
temp = [ 0 ] * 3
for j in range ( 3 ):
temp[j] = a[j]
for e in subset:
temp[e] + = x
getMinOperations(temp, b, ans,
num + 1 )
for x in mult:
temp = [ 0 ] * 3
for j in range ( 3 ):
temp[j] = a[j]
for e in subset:
temp[e] * = x
getMinOperations(temp, b, ans,
num + 1 )
return ans
if __name__ = = '__main__' :
a = [ 4 , 5 , 6 ]
b = [ 0 , 1 , 0 ]
ans = 3
ans = getMinOperations(a, b, ans, 0 )
print (ans_max)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int ans_max = 0;
static bool equal( int [] a, int [] b)
{
for ( int i = 0; i < 3; i++)
{
if (a[i] != b[i])
{
return false ;
}
}
return true ;
}
static int mulFac( int a, int b, int c, int d)
{
if (b != a && (d - c) % (b - a) == 0)
{
return (d - c) / (b - a);
}
else
{
return 1;
}
}
static void getMinOperations( int [] a, int [] b, int ans, int num)
{
if (num >= ans)
{
return ;
}
if (equal(a, b))
{
ans = Math.Min(ans, num);
ans_max = ans;
return ;
}
if (num >= 2)
{
return ;
}
HashSet< int > ad = new HashSet< int >();
ad.Add(b[0] - a[0]);
ad.Add(b[1] - a[1]);
ad.Add(b[2] - a[2]);
HashSet< int > mult = new HashSet< int >();
for ( int i = 0; i < 3; i++)
{
if (a[i] != 0 && b[i] % a[i] == 0)
{
mult.Add(b[i] / a[i]);
}
}
mult.Add(mulFac(a[0], a[1], b[0], b[1]));
mult.Add(mulFac(a[2], a[1], b[2], b[1]));
mult.Add(mulFac(a[0], a[2], b[0], b[2]));
mult.Add(0);
for ( int mask = 1; mask <= 7; mask++)
{
List< int > subset= new List< int >();
for ( int j = 0; j < 3; j++)
{
if ((mask & (1 << j)) != 0)
{
subset.Add(j);
}
}
foreach ( int x in ad)
{
int [] temp = new int [3];
for ( int j = 0; j < 3; j++)
{
temp[j] = a[j];
}
foreach ( int e in subset)
{
temp[e] += x;
}
getMinOperations(temp, b, ans,num + 1);
}
foreach ( int x in mult)
{
int [] temp = new int [3];
for ( int j = 0; j < 3; j++)
{
temp[j] = a[j];
}
foreach ( int e in subset)
{
temp[e] *= x;
}
getMinOperations(temp, b,ans, num + 1);
}
}
}
static public void Main ()
{
int [] a = { 4, 5, 6 };
int [] b = { 0, 1, 0 };
int ans = 3;
getMinOperations(a, b, ans, 0);
Console.WriteLine(ans_max);
}
}
|
Javascript
<script>
let ans_max = 0;
function equal(a, b)
{
for (let i = 0; i < 3; i++)
{
if (a[i] != b[i])
{
return false ;
}
}
return true ;
}
function mulFac(a, b, c, d)
{
if (b != a && (d - c) % (b - a) == 0)
{
return (d - c) / (b - a);
}
else
{
return 1;
}
}
function getMinOperations(a, b, ans, num)
{
if (num >= ans)
{
return ;
}
if (equal(a, b))
{
ans = Math.min(ans, num);
ans_max = ans;
return ;
}
if (num >= 2)
{
return ;
}
let ad = new Set();
ad.add(b[0] - a[0]);
ad.add(b[1] - a[1]);
ad.add(b[2] - a[2]);
let mult = new Set();
for (let i = 0; i < 3; i++)
{
if (a[i] != 0 && b[i] % a[i] == 0)
{
mult.add(b[i] / a[i]);
}
}
mult.add(mulFac(a[0], a[1],
b[0], b[1]));
mult.add(mulFac(a[2], a[1],
b[2], b[1]));
mult.add(mulFac(a[0], a[2],
b[0], b[2]));
mult.add(0);
for (let mask = 1; mask <= 7; mask++)
{
let subset = [];
for (let j = 0; j < 3; j++)
{
if ((mask & (1 << j)) != 0)
{
subset.push(j);
}
}
for (let x of ad.values())
{
let temp = new Array(3);
for (let j = 0; j < 3; j++)
{
temp[j] = a[j];
}
for (let e = 0; e < subset.length; e++)
{
temp[subset[e]] += x;
}
getMinOperations(temp, b, ans,
num + 1);
}
for (let x of mult.values())
{
let temp = new Array(3);
for (let j = 0; j < 3; j++)
{
temp[j] = a[j];
}
for (let e = 0; e < subset.length; e++)
{
temp[subset[e]] *= x;
}
getMinOperations(temp, b,
ans, num + 1);
}
}
}
let a = [ 4, 5, 6 ];
let b = [ 0, 1, 0 ];
let ans = 3;
getMinOperations(a, b, ans, 0);
document.write(ans_max);
</script>
|
Time Complexity: O(1) Auxiliary Space: O(1)
|