-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2429.cpp
More file actions
28 lines (27 loc) · 696 Bytes
/
2429.cpp
File metadata and controls
28 lines (27 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int minimizeXor(int num1, int num2) {
int count = __builtin_popcount(num2);
vector<bool> binary(32, false);
int res = 0;
int index = 31;
while (num1) {
if (num1 & 1) binary[index] = true;
num1 >>= 1;
index--;
}
for (int i = 0; i < 32; ++i) {
if (count > 0 && binary[i]) {
res += (1 << (31 - i));
count--;
}
}
for (int i = 31; i >= 0; --i) {
if (count > 0 && !binary[i]) {
res += (1 << (31 - i));
count--;
}
}
return res;
}
};