Untitled

 avatar
user_5668965
c_cpp
13 days ago
768 B
1
Indexable
Never
#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n, k;
    std::cin >> n >> k;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    int ans = k;
    for (int i = 0; i < n; i++) {
        ans = std::min(ans, (k - a[i] % k) % k);
    }
    if (k == 4) {
        int even = std::count_if(a.begin(), a.end(),
            [&](int x) {
                return x % 2 == 0;
            });
        ans = std::min(ans, std::max(0, 2 - even));
    }
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}
Leave a Comment