Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Divisibility by 2^n

#include <bits/stdc++.h>
using namespace std;

int exponent_of_two(int num) {
    int exp = 0;
    while ((num % 2) == 0) {
        exp++;
        num /= 2;
    }
    return exp;
}

int solve(vector<int> &a) {
    int n = a.size();

    int have = 0;
    vector<int> indices_power;
    for (int i = 0; i < n; i++) {
        have += exponent_of_two(a[i]);
        indices_power.push_back(exponent_of_two(i + 1));
    }

    sort(indices_power.begin(), indices_power.end(), greater<int>());
    int want = max(0, n - have);
    if (want == 0) {
        return 0;
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans++;
        want -= indices_power[i];
        if (want <= 0) {
            return ans;
        }
    }
    return -1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        cout << solve(a) << "\n";
    }
    return 0;
}