2429. Minimize XOR Medium
1/**
2 * [2429] Minimize XOR
3 *
4 * Given two positive integers num1 and num2, find the integer x such that:
5 *
6 * x has the same number of set bits as num2, and
7 * The value x XOR num1 is minimal.
8 *
9 * Note that XOR is the bitwise XOR operation.
10 * Return the integer x. The test cases are generated such that x is uniquely determined.
11 * The number of set bits of an integer is the number of 1's in its binary representation.
12 *
13 * Example 1:
14 *
15 * Input: num1 = 3, num2 = 5
16 * Output: 3
17 * Explanation:
18 * The binary representations of num1 and num2 are 0011 and 0101, respectively.
19 * The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
20 *
21 * Example 2:
22 *
23 * Input: num1 = 1, num2 = 12
24 * Output: 3
25 * Explanation:
26 * The binary representations of num1 and num2 are 0001 and 1100, respectively.
27 * The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= num1, num2 <= 10^9
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/minimize-xor/
38// discuss: https://leetcode.com/problems/minimize-xor/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn minimize_xor(num1: i32, num2: i32) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2429() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.