1419C - Killjoy

Editorial is pretty nice.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int solve(vi a, int x)
{
    if (all_of(begin(a), end(a), [x](int ai) { return ai == x; }))
        return 0;
    if (accumulate(begin(a), end(a), 0LL) - (int)(a).size() * x == 0 or find(begin(a), end(a), x) != a.end())
        return 1;
    return 2;
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
    {
        int n, x;
        cin >> n >> x;
        vi a(n);
        for (auto &ai : a)
            cin >> ai;
        cout << solve(a, x) << endl;
    }
    return 0;
}