December 2021

A2 - New Year Frogs (Hard Version)

Solution

\[\]

Implementation in C++

#include <bits/stdc++.h>

using namespace std;

int main() {
    int t, a, b;
    cin>>t;
    
    while (t--) {
        cin>>a>>b;
        
        if (a > b || (a+b)%2)
            cout<<"-1\n";
        else
            cout<<(a+b)/2<<"\n";
    }
    
    return 0;
}
\[\]

Implementation in Java

import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int t, a, b;
        
        t = scan.nextInt();
        
        while (t-- > 0) {
            a = scan.nextInt();
            b = scan.nextInt();
            
            if (a > b || (a+b)%2 != 0)
                System.out.println(-1);
            else
                System.out.println((a+b)/2);
        }
    }
}
\[\]

Implementation in Python

for _ in range(int(input())):
    a, b = map(int, input().split())
    if a > b or (a+b)%2 != 0:
        print(-1)
    else:
        print((a+b)//2)
\[\]

Contest Material