-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_Minimize_XOR.cpp
93 lines (73 loc) · 2.39 KB
/
15_Minimize_XOR.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 2429. Minimize XOR
// Given two positive integers num1 and num2, find the positive integer x such that:
// x has the same number of set bits as num2, and
// The value x XOR num1 is minimal.
// Note that XOR is the bitwise XOR operation.
// Return the integer x. The test cases are generated such that x is uniquely determined.
// The number of set bits of an integer is the number of 1's in its binary representation.
// Example 1:
// Input: num1 = 3, num2 = 5
// Output: 3
// Explanation:
// The binary representations of num1 and num2 are 0011 and 0101, respectively.
// The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
// Example 2:
// Input: num1 = 1, num2 = 12
// Output: 3
// Explanation:
// The binary representations of num1 and num2 are 0001 and 1100, respectively.
// The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
// Constraints:
// 1 <= num1, num2 <= 109
class Solution
{
public:
int countSetBits(int num)
{
int count = 0;
while (num)
{
count += num & 1;
num >>= 1;
}
return count;
}
int addSetBits(int num, int bitsToAdd)
{
int bitPos = 0;
while (bitsToAdd > 0)
{
while ((num >> bitPos) & 1)
++bitPos;
num |= (1 << bitPos);
--bitsToAdd;
}
return num;
}
int removeSetBits(int num, int bitsToRemove)
{
while (bitsToRemove > 0)
{
num &= (num - 1);
--bitsToRemove;
}
return num;
}
int minimizeXor(int num1, int num2)
{
int setBitsNum1 = countSetBits(num1);
int setBitsNum2 = countSetBits(num2);
if (setBitsNum1 == setBitsNum2)
return num1;
if (setBitsNum1 < setBitsNum2)
return addSetBits(num1, setBitsNum2 - setBitsNum1);
return removeSetBits(num1, setBitsNum1 - setBitsNum2);
}
};
// CountSetBits: Counts the number of set bits in a number.
// AddSetBits: Adds the required set bits by setting the least significant unset bits.
// RemoveSetBits: Removes extra set bits by unsetting the least significant set bits.
// It adjusts num1 based on whether it has fewer or more set bits compared to num2.
// Complexity
// Time complexity: O(32) (constant time for 32-bit integers).
// Space complexity: O(1).