GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Wednesday, 24 August 2022

[Solution] Make A and B equal CodeChef Solution



Problem

Chef is given two arrays A and B of length N each.

In one operation Chef can choose one element of A and one element of B and increase them by 1.

More formally: Chef can pick two integers i, j (1\le i, j \le N) and increment A_i and B_j by 1.

Determine the minimum number of operations required to make A and B equal.

Output -1 if it is not possible to make A and B equal.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains a single integer N - denoting the length of arrays A and B.
    • The second line of each test case contains N space separated integers A_1, A_2, A_3, \dots A_N - denoting the array A.
    • The third line of each test case contains N space separated integers B_1, B_2, B_3, \dots B_N - denoting the array B.

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

Output Format

For each test case, output the minimum number of operations to make A and B equal or -1 if they cannot be made equal.


Explanation:

Test case 1: We can choose i = 1 and j = 2 and increment A_i and B_j by 1. Thus, both arrays become [2, 2] and are equal. We require only 1 operation to make these arrays equal. It can be proven that the arrays cannot be made equal in less than one operation.

Test case 2: Both the arrays cannot be made equal using any number of operations.

Test case 3: We perform 2 operations as follows:

  • Choose i = 1, j = 3: The arrays become A = [5, 6, 8] and B = [5, 7, 7].
  • Choose i = 2, j = 3: The arrays become A = [5, 7, 8] and B = [5, 7, 8].

Thus, both arrays can be made equal using 2 operations.

No comments:

Post a Comment