Two equations You are given the following equations: • a+b=x • a XOR b = y You are given Q queries containing the values
Posted: Tue Jul 12, 2022 8:05 am
Approach You must determine the value of a and b (if exists) such that it meets the given conditions: e If you choose a = 1 and b = 5 then, (a + b) = (1 + 5) = 6 = x • (a xor b) = (1 xor 5) = 4 = y e Therefore the answer is a = 1 and b = 5. Function description 32°C Complete the solve function provided in the editor. This function takes the following 2 parameters: x. Represents the first integer value • y. Represents the second integer value Input format Note: This is the input format that you must use to provide custom Input (available above the Compile and Test button). • The first line of input contains Q denoting the number of queries, • Each of the next Q lines contains two space-separated integers x and y.
Output format For each query, print the space-separated integer solution (a, b) or -1 on a new line. Notes • If there exists more than one solution, then print the pair (a, b) with the smallest possible value of a. • If there exists no solution, then print -1. Constraints 1≤Q<106 0≤x, y < 1018 Code snippets (also called starter code/boilerplate code) This question has code snippets for C, CPP, Java, and Python. Sample input 5 2 42 34 Sample output
Explanation The first line represents the number of test cases, f = 2 The first test case x = 4 and y = 2 a+b = 4 a XOR b = 2 a = 1 and b = 3 are the smallest pairs that satisfy the given two equations. pairs The second test case x = 3 and y = 4 The answer does not exist. Hence output -1.
1 #include<stdio.h> 2 #include<stdbool.h> 3 #include<malloc.h> 5 7 8 9 10 11 12 13 15 16 17 18 19 void solve (long long x, long long y) { // write your code here int main() { int T; 21 } scanf("%d", &T); for(int t_i = 0; t_i < T; t_ií) { } long long x; scanf("%lld", &x); long long y; scanf("%lld", &y); solve(x, y);