ECJN201-Editorial

PROBLEM LINK:

Practice

Author: Akash Kumar Bhagat
Tester: Sandeep Singh,Arnab Chanda
Editorialist: Akash Kumar Bhagat

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Just simple maths

PROBLEM:

Given a triple, you can perform only one task on it, i.e. choose a number d>0 and a subset of the triple and add d to each element in that particular subset.

You are given an initial triple (p,q,r) and a target triple (a,b,c). Find the maximum number of operations needed to transform (p,q,r) into (a,b,c) or say the conversion is impossible.

EXPLANATION:

Since we have to find the Maximum number of transformations to convert the given triple to the target triple, we could just add 1(>0) one of the element each time of the transformation. Hence the answer to the problem would be the sum of the difference between the corresponding target triple and give triple. If in case the difference is negative, the transformation is impossible.

SOLUTIONS:

Python 3.7
for _ in range(int(input())):
    a=[int(i) for i in input().split()]
    b=[int(i) for i in input().split()]
    ans=0;boo=True
    for i in range(3):
        t=b[i]-a[i]
        if(t<0):    boo=False
        ans+=t
    if(boo):
        print(ans)
    else:
        print(-1)
CPP
#include <bits/stdc++.h>
#define ll long long int
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define scanarr(a,b,c) for( i=b;i<c;i++)cin>>a[i]
#define showarr(a,b,c) for( i=b;i<c;i++)cout<<a[i]<<' '
#define ln cout<<'\n'
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAX 100005
using namespace std;
////////////////////////////////////////////////////////////////CODE STARTS HERE////////////////////////////////////////////////////////////////
 
void solve(){
 
    int a, b, c, p, q, r;
    
    cin >> p >> q >> r;
    cin >> a >> b >> c;
 
    if((a - p) < 0 || (b - q) < 0 || (c - r) < 0)
        cout << -1 << endl;
    else
        cout << (a - p) + (b - q) + (c - r) << endl; 
}
    
 
int main(){
 
 
    int t;
    cin >> t;
    while(t--)
        solve();
} 
2 Likes