You are given an array consisting of n integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.
\ For example, if the array is [1,2,4,3,2,3,5,4], we can color it as follows: [1,2,4,3,2,3,5,4], where the sum of the blue elements is 6 and the sum of the red elements is 18.\ \ Input
The first line contains an integer t (1≤t≤1000) — the number of test cases.\ \ Each test case begins with a line containing an integer n (2≤n≤50) — the length of the array a.\ \ The next line contains n integers a1,a2,…,an (1≤ai≤50) — the elements of the array a.\ \ Output
For each test case, output "YES" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and "NO" otherwise.\ \ You can output "Yes" and "No" in any case (for example, the strings "yES", "yes", and "Yes" will be recognized as correct answers).c++代码内容:Here is a C++ code to solve the problem:\ \

#include <iostream>\
#include <vector>\
\\
using namespace std;\
\\
bool canColorArray(vector<int>& arr) {\
    int n = arr.size();\
    int sum = 0;\
    for (int i = 0; i < n; i++) {\
        sum += arr[i];\
    }\\
    // If the sum of the array is even, it is always possible to color the array in two colors\
    if (sum % 2 == 0) {\
        return true;\
    }\\
    // If the sum of the array is odd, we need to check if there are at least two odd numbers in the array\
    int countOdd = 0;\
    for (int i = 0; i < n; i++) {\
        if (arr[i] % 2 == 1) {\
            countOdd++;\
        }\\
    }\\
    // If there are at least two odd numbers, it is possible to color the array in two colors\
    if (countOdd >= 2) {\
        return true;\
    }\\
    return false;\
}\\
\\
int main() {\
    int t;\
    cin >> t;\
    while (t--) {\
        int n;\
        cin >> n;\
        vector<int> arr(n);\
        for (int i = 0; i < n; i++) {\
            cin >> arr[i];\
        }\\
        if (canColorArray(arr)) {\
            cout << "YES" << endl;\
        } else {\
            cout << "NO" << endl;\
        }\\
    }\\
    return 0;\
}\\
```\
\\
Explanation:\\
- The function `canColorArray` takes an array as input and checks if it is possible to color the array in two colors based on the rules given in the problem statement.\\
- The function first calculates the sum of all elements in the array.\\
- If the sum is even, it is always possible to color the array in two colors, so the function returns true.\\
- If the sum is odd, the function counts the number of odd numbers in the array.\\
- If there are at least two odd numbers, it is possible to color the array in two colors, so the function returns true.\\
- Otherwise, the function returns false.\\
- In the main function, we read the number of test cases and for each test case, we read the length of the array and the elements of the array.\\
- We then call the `canColorArray` function for each test case and print "YES" if it returns true, and "NO" otherwise.
Coloring Array Elements for Equal Parity Sums

原文地址: https://www.cveoy.top/t/topic/qeg2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录