Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- // this function adds/subtracts every set bit of a number (OR operation)
- void BitFrequency(int x, int* frequency, int add_sub) {
- for (int i = 0; i < 32; i++) {
- if (x & (1 << i)) {
- frequency[i] += add_sub;
- }
- }
- }
- // this function converts to decimal representation from a binary representation
- int Calculate(int frequency[]) {
- int ans = 0;
- for (int i = 0; i < 32; i++) {
- if (frequency[i] >= 1) {
- ans += (1 << i);
- }
- }
- return ans;
- }
- int main() {
- int n;
- cin >> n;
- int a[n];
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- int frequency[32] = {0};
- for (int i = 0; i < n; i++) {
- BitFrequency(a[i], frequency, 1);
- }
- int ans = Calculate(frequency);
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement