CodeChef June Long Challenge 2020

Problem Description

These type of Questions are generally appear in Long challenge due to their various edge cases involve which take more time to find out.

Examples

Example 1: 

2
3 5 7
6 5 10
8 6 3
9 7 8

Examples

Example 1: 

2
3 5 7
6 5 10
8 6 3
9 7 8

 

3 5 7




6 5 10

+3

Examples

Example 1: 

2
3 5 7
6 5 10
8 6 3
9 7 8

+3

 

3 5 7




6 5 10

 

8 6 3




9 7 4

 

 

 

9 7 8

+1

x2

So how to approach this Problem

Observe

So how to approach this Problem

Observe

1. We can only have 4 answers 0, 1, 2, and 3 for any tuple

So how to approach this Problem

Observe

1. We can only have 4 answers 0, 1, 2, and 3 for any tuple

2. Now take a look on these answers how we can get these answers.

So how to approach this Problem

Observe

1. We can only have 4 answers 0, 1, 2, and 3 for any tuple

2. Now take a look on these answers how we can get these answers.

Let's try to understand them one by one.

So how to approach this Problem

Case 1: when answer will be 0

Initial triple = Target Triple

So how to approach this Problem

Case 1: when answer will be 0

Initial triple = Target Triple

p = a

q = b

r = c

(1,2,3)

(1,2,3)

So how to approach this Problem

For all other answers(1,2,3) we will try to understand it by different scenarios.

So how to approach this Problem

Now there can be three possible cases:

CASE 1: where there is only one different number.

a) (1,2,3)  (2,2,3)

b) (1,2,3)  (1,4,3)

c) (1,2,3)  (1,2,4)

p != a

or

q != b

or

r != c

So how to approach this Problem

Now there can be three possible cases:

CASE 1: where there is only one different number.

a) (1,2,3)  (2,2,3)

b) (1,2,3)  (1,4,3)

c) (1,2,3)  (1,2,4)

CASE 2: where there are only two different number.

a) (1,2,3)  (2,5,3)

b) (1,2,3)  (1,5,4)

p != a

or

q != b

or

r != c

p != a & q != b

or

q != b & r != c 

or

p != a & r != c

So how to approach this Problem

Now there can be three possible cases:

CASE 1: where there is only one different number.

a) (1,2,3)  (2,2,3)

b) (1,2,3)  (1,4,3)

c) (1,2,3)  (1,2,4)

CASE 2: where there are only two different number.

a) (1,2,3)  (2,5,3)

b) (1,2,3)  (1,5,4)

CASE 3: where there are three different number.

a) (1,2,3)  (4,5,6)

p != a & q != b

or

q != b & r != c 

or

p != a & r != c

p != a

or

q != b

or

r != c

p != a & q != b & r != c

 

Case Wise solution

CASE 1: where there is only one different number.

a) (1,2,3)  (2,2,3)

b) (1,2,3)  (1,4,3)

c) (1,2,3)  (1,2,4)

No matter what answer will be 1 in this case.

Case Wise solution

CASE 2: where there are only two different number.

a) (1,2,3)  (2,5,3)

b) (1,2,3)  (1,5,4)

Answers possible for this case.

1

2

Case Wise solution

CASE 2: where there are only two different number.

 

Answers possible for this case.

1

2

If the two numbers that are different can be converted into corresponding target values in a single move.

if answer is not 1 it will be definitely 2.

Case Wise solution

CASE 2: where there are only two different number.

 

Answers possible for this case.

1

2

If the two numbers that are different can be converted into corresponding target values in a single move.

if answer is not 1 it will be definitely 2.

For example (2,3,4) -> (5,6,4)

Case Wise solution

CASE 2: where there are only two different number.

 

Answers possible for this case.

1

2

If the two numbers that are different can be converted into corresponding target values in a single move.

if answer is not 1 it will be definitely 2.

For example (2,3,4) -> (5,6,4)

Add 3 to the two different numbers hence answer would be 1.

+3

(5,6,4)

Case Wise solution

CASE 2: where there are only two different number.

 

Answers possible for this case.

1

2

If the two numbers that are different can be converted into corresponding target values in a single move.

if answer is not 1 it will be definitely 2.

For example (2,3,4) -> (6,9,4)

Multiply by 3 to the two different numbers hence answer would be 1.

x3

(6,9,4)

typedef long long ll
bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
// When there are two different numbers
ll case1()
{
	if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
		return 1;
	return 2;
}

Returns true if we can convert the two different numbers in a single move

for this Example: (1,2,3) -> (3,4,3)

Here N = 1, M = 2

n = 3, m = 4

N

n

M

m

Let's move to our next Case

When all three numbers are different.

p != a

q != b

r != c

Let's move to our next Case

When all three numbers are different.

p != a

q != b

r != c

There are three cases that could be possible:

when answer is 1

when answer is 2

when answer is 3

 

Let's move to our next Case

When all three numbers are different.

There are three cases that could be possible:

when answer is 1

when answer is 2

when answer is 3

 

We will discuss all three cases one by one

Case 1: when the answer is 1

We need to convert all three numbers to target numbers

Case 1: when the answer is 1

We need to convert all three numbers to target numbers

We need a single operation either add a number or multiply a number so that three of them converts to the target numbers

Implies

Case 1: when the answer is 1

We need to convert all three numbers to target numbers

We need a single operation either add a number or multiply a number so that three of them converts to the target numbers

Implies

Implies

p -> a

q -> b

r -> c

(p,q,r) + d => a, b, c

or

(p,q,r) X d => a, b, c

Code for this case

if ((p - a == q - b) && (q - b == r - c))
	return 1;
if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
	return 1;

p != a

q != b

r != c

Case 1: when the answer is 1 (Code)

Code for this case

if ((p - a == q - b) && (q - b == r - c))
	return 1;
if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
	return 1;

p != a

q != b

r != c

Case 1: when the answer is 1 (Code)

p q r

a b c

Code for this case

if ((p - a == q - b) && (q - b == r - c))
	return 1;
if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
	return 1;

p != a

q != b

r != c

Case 1: when the answer is 1 (Code)

p q r

a b c

Code for this case

if ((p - a == q - b) && (q - b == r - c))
	return 1;
if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
	return 1;

p != a

q != b

r != c

Case 1: when the answer is 1 (Code)

p q r

a b c

Code for this case

if ((p - a == q - b) && (q - b == r - c))
	return 1;
if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
	return 1;

p != a

q != b

r != c

Case 1: when the answer is 1 (Code)

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

This is rather a long case

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)

(p + x) * d = a \newline (q + x) * d = b \newline (r + x) * d = c \newline

OR

(p * d) + x = a \newline (q * d) + x = b \newline (r * d) + x = c \newline

Example

4 2 3

5 3 4

10 6 8

+1

x2

Example

4 2 3

8 4 6

10 6 8

x2

+2

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)

(p + x) * d = a \newline (q + x) * d = b \newline (r + x) * d = c \newline

OR

(p * d) + x = a \newline (q * d) + x = b \newline (r * d) + x = c \newline
p * d - q * d = a - b => d = (a - b) / (p - q)

-

p * d - q * d = a - b => d = (a - b) / (p - q)

-

p * d - q * d = a - b => d = (a - b) / (p - q)

-

p * d - q * d = a - b => d = (a - b) / (p - q)

-

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)

(p + x) * d = a \newline (q + x) * d = b \newline (r + x) * d = c \newline

OR

(p * d) + x = a \newline (q * d) + x = b \newline (r * d) + x = c \newline
p * d - q * d = a - b => d = (a - b) / (p - q)
p * d - q * d = a - b => d = (a - b) / (p - q)
p * d - q * d = a - b => d = (a - b) / (p - q)
p * d - q * d = a - b => d = (a - b) / (p - q)
q * d - r * d = b - c => d = (b - c) / (q - r)
q * d - r * d = b - c => d = (b - c) / (q - r)
p * d - r * d = a - c => d = (a - c) / (p - r)
p * d - r * d = a - c => d = (a - c) / (p - r)

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)(Code)

if (
p != q && q != r && r != p && (a-b)%(p-q) == 0 && (b-c)%(q-r) == 0 && (a-c)%(p-r) == 0 
				&& 
((a-b)/(p-q) == (b-c)/(q-r)) && ((b-c)/(q-r) == (a-c)/(p-r))
)
	return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)(Code)

if (
p != q && q != r && r != p && (a-b)%(p-q) == 0 && (b-c)%(q-r) == 0 && (a-c)%(p-r) == 0 
				&& 
((a-b)/(p-q) == (b-c)/(q-r)) && ((b-c)/(q-r) == (a-c)/(p-r))
)
	return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.2)(Code)

if (
p != q && q != r && r != p && (a-b)%(p-q) == 0 && (b-c)%(q-r) == 0 && (a-c)%(p-r) == 0 
				&& 
((a-b)/(p-q) == (b-c)/(q-r)) && ((b-c)/(q-r) == (a-c)/(p-r))
)
	return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

First check if any two numbers can be converted to target numbers in one move and third number with extra move.

p q 

a b

r

c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

First check if any two numbers can be converted to target numbers in one move and third number with extra move.

p q 

a b

r

c

Example 

3 5 6

15 25 7

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

First check if any two numbers can be converted to target numbers in one move and third number with extra move.

p q 

a b

r

c

Example 

3 5   6

15 25  7

x5

+1

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

typedef long long ll;
bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}

if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

typedef long long ll;
bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}

if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

typedef long long ll;
bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}

if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

a - p = d

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

a - p = d

p q r

d

+

a, q+d, r+d

e

+

or

e

x

a, b,c

Example

3 5 6

5 7 8

5 14 16

+2

x2

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

b - q = d

p q r

d

+

a, q+d, r+d

e

+

or

e

x

a, b,c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

c - r = d

p q r

d

+

a, q+d, r+d

e

+

or

e

x

a, b,c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

a - p = d

p q r

d

+

p, s, t

a, b,c

e

+

or

e

x

s = b - d

t = c - d

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)

p q r

a b c

d

a - p = d

p q r

d

+

p, s, t

a, b,c

e

+

or

e

x

s = b - d

t = c - d

Example

3 5 7

3 10 14

5 12 16

x2

+2

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.3)(code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if (check1(q+a-p, r+a-p, b,c) || check1(p+c-r, q+c-r, a,b) || check1(p+b-q, r+b-q, a,c))
	return 2;
if (check1(q, r, b-(a-p),c-(a-p)) || check1(p, q, a-(c-r),b-(c-r)) || check1(p, r, a-(b-q),c-(b-q)))
	return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)

p q r

a b c

d

a - p = d

p q r

d

+

a, q+d, r

e

+

or

e

x

a, b,c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)

p q r

a b c

d

a - p = d

p q r

d

+

a, q+d, r

e

+

or

e

x

a, b,c

Example

1 2 3

4 15 9

 

1 2 3

3

+

4, 5, 3

3

x

4, 15, 9

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)

p q r

a b c

d

a - p = d

p q r

d

+

a, q+d, r

e

+

or

e

x

a, b,c

p

q

r

q

p

r

r

p

q

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)

p q r

a b c

d

a - p = d

p q r

p, s, c

a, b,c

Example

1 2 3

4 9 9

 

1 2 3

3

x

1, 6, 9

3

+

4, 9, 9

d

+

e

+

or

e

x

s = b - d

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)

p q r

a b c

d

a - p = d

p q r

p, s, c

a, b,c

d

+

e

+

or

e

x

s = b - d

p

q

r

q

p

r

r

p

q

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)(code)

bool check1(ll N, ll M, ll n, ll m)
{
	if (N - n == M - m)
		return true;
	else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
		return true;
	return false;
}

if (check1(q+a-p, r, b,c) || check1(p+c-r, q, a,b) || check1(p+b-q, r, a,c) || check1(q, r+a-p, b,c) || check1(p, q+c-r, a,b) || check1(p, r+b-q, a,c))
    return 2;
if (check1(q, r, b-(a-p),c) || check1(p, q, a-(c-r),b) || check1(p, r, a-(b-q),c) || check1(q, r, b,c-(a-p)) || check1(p, q, a,b-(c-r)) || check1(p, r, a,c-(b-q)))
    return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.4)(code)

bool check1(ll N, ll M, ll n, ll m)
{
	if (N - n == M - m)
		return true;
	else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
		return true;
	return false;
}

if (check1(q+a-p, r, b,c) || check1(p+c-r, q, a,b) || check1(p+b-q, r, a,c) || check1(q, r+a-p, b,c) || check1(p, q+c-r, a,b) || check1(p, r+b-q, a,c))
    return 2;
if (check1(q, r, b-(a-p),c) || check1(p, q, a-(c-r),b) || check1(p, r, a-(b-q),c) || check1(q, r, b,c-(a-p)) || check1(p, q, a,b-(c-r)) || check1(p, r, a,c-(b-q)))
    return 2;

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.5)

p q r

a b c

d

a/p = d

p q r

d

x

a, qxd, rxd

e

+

or

e

x

a, b,c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.5)

p q r

a b c

d

a/p = d

p q r

d

x

a, qxd, rxd

e

+

or

e

x

a, b,c

Example

3 5 7

6 10 14

6 12 16

x2

+2

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && check1(q*(a/p),r*(a/p),b,c)) 
			|| 
(q != 0 && b%q == 0 && check1(p*(b/q),r*(b/q),a,c)) 
			|| 
(r != 0 && c%r == 0 && check1(p*(c/r),q*(c/r),a,b))
)
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && check1(q*(a/p),r*(a/p),b,c)) 
			|| 
(q != 0 && b%q == 0 && check1(p*(b/q),r*(b/q),a,c)) 
			|| 
(r != 0 && c%r == 0 && check1(p*(c/r),q*(c/r),a,b))
)
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && check1(q*(a/p),r*(a/p),b,c)) 
			|| 
(q != 0 && b%q == 0 && check1(p*(b/q),r*(b/q),a,c)) 
			|| 
(r != 0 && c%r == 0 && check1(p*(c/r),q*(c/r),a,b))
)
    return 2;

p q r

a b c

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

p q r

a b c

d

a/p = d

p q r

d

x

a, qxd, r

e

+

or

e

x

a, b,c

Example 

2 3 4

6 9 4

6 11 6

x3

+2

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && (check1(q*(a/p),r,b,c) || check1(q,r*(a/p),b,c))) 
		||
(q != 0 && b%q == 0 && (check1(p*(b/q),r,a,c) || check1(p,r*(b/q),a,c))) 
		|| 
(r != 0 && c%r == 0 && (check1(p*(c/r),q,a,b) || check1(p,q*(c/r),a,b)))
)
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && (check1(q*(a/p),r,b,c) || check1(q,r*(a/p),b,c))) 
		||
(q != 0 && b%q == 0 && (check1(p*(b/q),r,a,c) || check1(p,r*(b/q),a,c))) 
		|| 
(r != 0 && c%r == 0 && (check1(p*(c/r),q,a,b) || check1(p,q*(c/r),a,b)))
)
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if ((p != 0 && a%p == 0 && (check1(q*(a/p),r,b,c) || check1(q,r*(a/p),b,c))) 
		||
(q != 0 && b%q == 0 && (check1(p*(b/q),r,a,c) || check1(p,r*(b/q),a,c))) 
		|| 
(r != 0 && c%r == 0 && (check1(p*(c/r),q,a,b) || check1(p,q*(c/r),a,b)))
)
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (1.6) 

p q r

a b c

d

a/p = d

p q r

d

x

p, s, c

e

+

or

e

x

a, b,c

Example 

2 3 4

2 5 6

4 10 6

+2

x2

b = s*d

=> s = b/d

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if (p != 0 && a%p == 0 && a != 0 && ((b%(a/p) == 0 && check1(q,r,b/(a/p),c)) || (c%(a/p) == 0 && check1(q,r,b,c/(a/p))))) 
    return 2;
if (q != 0 && b%q == 0 && b != 0 && ((a%(b/q) == 0 && check1(p,r,a/(b/q),c)) || (c%(b/q) == 0 && check1(p,r,a,c/(b/q)))))
    return 2;
if (r != 0 && c%r == 0 && c != 0 && ((a%(c/r) == 0 && check1(p,q,a/(c/r),b)) || (b%(c/r) == 0 && check1(p,q,a,b/(c/r)))))
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if (p != 0 && a%p == 0 && a != 0 && ((b%(a/p) == 0 && check1(q,r,b/(a/p),c)) || (c%(a/p) == 0 && check1(q,r,b,c/(a/p))))) 
    return 2;
if (q != 0 && b%q == 0 && b != 0 && ((a%(b/q) == 0 && check1(p,r,a/(b/q),c)) || (c%(b/q) == 0 && check1(p,r,a,c/(b/q)))))
    return 2;
if (r != 0 && c%r == 0 && c != 0 && ((a%(c/r) == 0 && check1(p,q,a/(c/r),b)) || (b%(c/r) == 0 && check1(p,q,a,b/(c/r)))))
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 2 (Code)

bool check1(ll N, ll M, ll n, ll m)
{
    if (N - n == M - m)
        return true;
    else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
        return true;
    return false;
}
if (p != 0 && a%p == 0 && a != 0 && ((b%(a/p) == 0 && check1(q,r,b/(a/p),c)) || (c%(a/p) == 0 && check1(q,r,b,c/(a/p))))) 
    return 2;
if (q != 0 && b%q == 0 && b != 0 && ((a%(b/q) == 0 && check1(p,r,a/(b/q),c)) || (c%(b/q) == 0 && check1(p,r,a,c/(b/q)))))
    return 2;
if (r != 0 && c%r == 0 && c != 0 && ((a%(c/r) == 0 && check1(p,q,a/(c/r),b)) || (b%(c/r) == 0 && check1(p,q,a,b/(c/r)))))
    return 2;

p q r

a b c

p q r

a b c

OR

Code for this case

p != a

q != b

r != c

Case 1: when the answer is 3

Thats'it we have covered all the cases whee answer could be 2 now if our input does not satisfy any of these cases that means we will need minimum 3 operations.

Complete Code

ll p,q,r;
ll a,b,c;

const int maxN = 1e5;
ll arr[maxN];
ll arr1[maxN];

bool check1(ll N, ll M, ll n, ll m)
{
	if (N - n == M - m)
		return true;
	else if (N != 0 && M != 0 && n % N == 0 && m % M == 0 && n/N == m/M)
		return true;
	return false;
}

ll case2()
{
	if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
		return 1;
	return 2;
}

ll case3()
{
	ll g,h;
	for (int i = 1; i < min(a,b); ++i)
	{
		if (i >= 1000)
			break;
		if (p % i == 0)
		{
			g = p;
			break;
		}
		else 
			h = i;

	}
	if ((p - a == q - b) && (q - b == r - c))
		return 1;
	if (p != 0 && q != 0 && r != 0 && a%p == 0 && b%q == 0 && c%r == 0 && a/p == b/q && b/q == c/r)
		return 1;
	if (p != q && q != r && r != p && (a-b)%(p-q) == 0 && (b-c)%(q-r) == 0 && (a-c)%(p-r) == 0 && ((a-b)/(p-q) == (b-c)/(q-r)) && ((b-c)/(q-r) == (a-c)/(p-r)))
		return 2;
	if (check1(p,q,a,b) || check1(p,r,a,c) || check1(q,r, b,c))
		return 2;
	if (check1(q+a-p, r+a-p, b,c) || check1(p+c-r, q+c-r, a,b) || check1(p+b-q, r+b-q, a,c))
		return 2;
	if (check1(q, r, b-(a-p),c-(a-p)) || check1(p, q, a-(c-r),b-(c-r)) || check1(p, r, a-(b-q),c-(b-q)))
		return 2;
	if ((p != 0 && a%p == 0 && check1(q*(a/p),r*(a/p),b,c)) || (q != 0 && b%q == 0 && check1(p*(b/q),r*(b/q),a,c)) || (r != 0 && c%r == 0 && check1(p*(c/r),q*(c/r),a,b)))
		return 2;
	if (check1(q+a-p, r, b,c) || check1(p+c-r, q, a,b) || check1(p+b-q, r, a,c) || check1(q, r+a-p, b,c) || check1(p, q+c-r, a,b) || check1(p, r+b-q, a,c))
		return 2;
	if (check1(q, r, b-(a-p),c) || check1(p, q, a-(c-r),b) || check1(p, r, a-(b-q),c) || check1(q, r, b,c-(a-p)) || check1(p, q, a,b-(c-r)) || check1(p, r, a,c-(b-q)))
		return 2;
	if ((p != 0 && a%p == 0 && (check1(q*(a/p),r,b,c) || check1(q,r*(a/p),b,c))) || (q != 0 && b%q == 0 && (check1(p*(b/q),r,a,c) || check1(p,r*(b/q),a,c))) || (r != 0 && c%r == 0 && (check1(p*(c/r),q,a,b) || check1(p,q*(c/r),a,b))))
		return 2;
	if (p != 0 && a%p == 0 && a != 0 && ((b%(a/p) == 0 && check1(q,r,b/(a/p),c)) || (c%(a/p) == 0 && check1(q,r,b,c/(a/p))))) 
		return 2;
	if (q != 0 && b%q == 0 && b != 0 && ((a%(b/q) == 0 && check1(p,r,a/(b/q),c)) || (c%(b/q) == 0 && check1(p,r,a,c/(b/q)))))
		return 2;
	if (r != 0 && c%r == 0 && c != 0 && ((a%(c/r) == 0 && check1(p,q,a/(c/r),b)) || (b%(c/r) == 0 && check1(p,q,a,b/(c/r)))))
		return 2;
	return 3;
}



ll solve()
{
	cin >> p >> q >> r;
	cin >> a >> b >> c;
	std::vector<int> diff;
	if (p != a)
		diff.push_back(0);
	if (q != b)
		diff.push_back(1);
	if (r != c)
		diff.push_back(2);
	if (diff.size() == 0)
		return 0;
	if (diff.size() == 1)
		return 1;

	if (diff.size() == 2)
		return case2();
	return case3();
}

int main()
{
	#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
	#endif

    ll tc;
    cin >> tc;
    for (ll i = 0;i < tc;i++)
    {
    	cout << solve() << "\n";
    }
    return 0;
}

A Thought About the Question

I know this question is all about finding out the different cases and then generalization of these cases.

 

 

It definitely takes time to finding out all the cases.

That's why it appears in Long Challenge.

 

 

This question teaches not to give up and try to figure out the different edge cases because that's what software Engineering Demands from you, that's what good companies look in to you.

Any Doubts

If you still have any doubts you can comment your doubts in the comment section.

If you have a better approach than this you can also comment it out.

Like, Share, Subscribe!

If you like our efforts Please Do Like this video, share it with your friends who was not able to solve this problem.

Also Do subscribe this channel because more video Editorials and coding related content is coming on this channel.

Like, Share, Subscribe!

QWERT

By gauravsahu

QWERT

  • 48