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 : Equal XOR

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

#define endl "\n"

void solve() {
    int n, k;
    cin >> n >> k;
    map<int, int> lfreq, rfreq;
    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;
        lfreq[num]++;
    }
    for (int i = 0; i < n; i++) {
        int num;
        cin >> num;
        rfreq[num]++;
    }
    set<int> lone, ltwo, rone, rtwo;
    for (auto &kv : lfreq) {
        if (kv.second == 2) {
            ltwo.insert(kv.first);
        } else {
            lone.insert(kv.first);
        }
    }
    for (auto &kv : rfreq) {
        if (kv.second == 2) {
            rtwo.insert(kv.first);
        } else {
            rone.insert(kv.first);
        }
    }

    vector<int> lhs, rhs;

    while (!ltwo.empty() && lhs.size() <= 2 * k - 2) {
        int ele = *ltwo.begin();
        ltwo.erase(ele);
        lhs.push_back(ele);
        lhs.push_back(ele);
    }

    while (!rtwo.empty() && rhs.size() <= 2 * k - 2) {
        int ele = *rtwo.begin();
        rtwo.erase(ele);
        rhs.push_back(ele);
        rhs.push_back(ele);
    }

    while ((int)lhs.size() < 2 * k || (int)rhs.size() < 2 * k) {
        if (!lone.empty() && !rone.empty()) {
            int ele = *lone.begin();
            lhs.push_back(ele);
            rhs.push_back(ele);
            lone.erase(ele);
            rone.erase(ele);
        }
    }

    for (auto &ele : lhs) {
        cout << ele << " ";
    }
    cout << endl;
    for (auto &ele : rhs) {
        cout << ele << " ";
    }
    cout << endl;
}

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

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}