Saturday, January 27, 2024

[Hackerearth] Hamming Sort

 

Problem Link    : Hamming Sort 
Category        : Add Hoc
Contest         : January Easy '24

#include<bits/stdc++.h>
using namespace std;
vector<int> solve (vector<int> arr, int k) {
   int n = arr.size();
   vector<int> marr;
   for (int i = 1; i < n; i++) {
        marr.push_back(arr[i]);
   }
   marr.push_back(k);
   k = arr[0];
   vector<pair<int, int>> cache;
   for (int i = 0; i < marr.size(); i++) {
        int val = marr[i];
        int xors = val ^ k;
        int bits = __builtin_popcount(xors);
        cache.push_back({bits, val}); 
   }
   sort(cache.begin(), cache.end());
   vector<int> ans;
   for (int i = 0; i < cache.size(); i++) {
        ans.push_back(cache[i].second);
   }
   return ans;
}
 
int main() {
 
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    for(int t_i = 0; t_i < T; t_i++)
    {
        int N;
        cin >> N;
        vector<int> arr(N);
        for(int i_arr = 0; i_arr < N; i_arr++)
        {
        	cin >> arr[i_arr];
        }
        int K;
        cin >> K;
 
        vector<int> out_;
        out_ = solve(arr, K);
        cout << out_[0];
        for(int i_out_ = 1; i_out_ < out_.size(); i_out_++)
        {
        	cout << " " << out_[i_out_];
        }
        cout << "\n";
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.