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 : Add, Divide and Floor

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

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

    sort(a.begin(), a.end());
    int min_element = a.front();
    int max_element = a.back();

    int operation_count = 0;
    vector<int> choosen_x;
    while (min_element != max_element) {
        operation_count++;
        int x;

        // Case similar to [3, 5]
        // If you add zero, it goes to [1, 2]
        // If you add one, it goes to [2, 3]
        if (min_element % 2 == 1 && max_element % 2 == 1) {
            x = 0;
        }

        // Case similar to [4, 6]
        // If you add zero, it goes to [2, 3]
        // If you add one, it goes to [2, 3]
        if (min_element % 2 == 0 && max_element % 2 == 0) {
            x = 0;
        }

        // Case similar to [3, 4]
        // If you add zero, it goes to [1, 2]
        // If you add one, it goes to [2, 2]
        if (min_element % 2 == 1 && max_element % 2 == 0) {
            x = 1;
        }

        // Case similar to [4, 5]
        // If you add zero, it goes to [2, 2]
        // If you add one, it goes to [2, 3]
        if (min_element % 2 == 0 && max_element % 2 == 1) {
            x = 0;
        }

        min_element = (min_element + x) / 2;
        max_element = (max_element + x) / 2;
        choosen_x.push_back(x);
    }

    cout << operation_count << endl;
    if (operation_count > 0 && operation_count <= n) {
        for (int i = 0; i < (int)choosen_x.size(); i++) {
            cout << choosen_x[i] << " ";
        }
        cout << "\n";
    }
}

int main() {
    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];
        }
        solve(a);
    }
    return 0;
}