Codeforces 1742A Sum Solution & Explanation

Difficulty : 800

Problem Description

You are given three integers a, b, and c. Determine if one of them is the sum of the other two.

Input

The first line contains a single integer t(1≤t≤9261) — the number of test cases.

The description of each test case consists of three integers a, b, c(0≤a,b,c≤20).

Output

For each test case, output “YES” if one of the numbers is the sum of the other two, and “NO” otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Examples

Input7
1 4 3
2 5 8
9 11 20
0 0 0
20 20 20
4 12 3
15 7 8
OutputYES
NO
YES
YES
NO
NO
YES

Note

In the first test case, 1+3=4.

In the second test case, none of the numbers is the sum of the other two.

In the third test case, 9+11=20.


Solution

In our solution, we use two variables to check the third number.

tmp1 and tmp2 represent two possible scenarios for the third number:

tmp1 indicates that the third number is obtained by adding the first and second numbers.
tmp2 indicates that the third number is obtained by finding the absolute difference between the first and second numbers.
In the case of tmp2, it reflects the idea that the third number is derived from subtracting the smaller of the first and second numbers from the larger one.

C# Solution

Solution1

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    
    var tmp1 = arr[0]+arr[1];
    var tmp2 = arr[0]>arr[1]?arr[0]-arr[1]:arr[1]-arr[0];

    
    Console.WriteLine(arr[2]==tmp1 || arr[2]==tmp2?"YES":"NO");
}

Or, we can solve it with Array.Sort, resulting in a worse time complexity.

Solution2

int n = int.Parse(Console.ReadLine());

for(int i=0; i<n; i++){
    int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    Array.Sort(arr);
    
    bool res = arr[2]-arr[1]-arr[0] == 0;
    Console.WriteLine(res?"YES":"NO");
}

Java Solution

Solution1

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();

        for (int i = 0; i < n; i++) 
        {
            int[] arr = Arrays.stream(scanner.nextLine().split(" "))
                          .mapToInt(Integer::parseInt)
                          .toArray();
            int tmp1 = arr[0]+arr[1];
            int tmp2 = arr[0]>arr[1]?arr[0]-arr[1]:arr[1]-arr[0];

             System.out.println(arr[2]==tmp1 || arr[2]==tmp2?"YES":"NO");
        }
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 1742A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *