3226. Number of Bit Changes to Make Two Integers Equal Easy

@problem@discussion
#Bit Manipulation



1/**
2 * [3226] Number of Bit Changes to Make Two Integers Equal
3 *
4 * You are given two positive integers n and k.
5 * You can choose any bit in the binary representation of n that is equal to 1 and change it to 0.
6 * Return the number of changes needed to make n equal to k. If it is impossible, return -1.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">n = 13, k = 4</span>
11 * Output: <span class="example-io">2</span>
12 * Explanation:<br />
13 * Initially, the binary representations of n and k are n = (1101)2 and k = (0100)2.<br />
14 * We can change the first and fourth bits of n. The resulting integer is n = (<u>0</u>10<u>0</u>)2 = k.
15 * </div>
16 * <strong class="example">Example 2:
17 * <div class="example-block">
18 * Input: <span class="example-io">n = 21, k = 21</span>
19 * Output: <span class="example-io">0</span>
20 * Explanation:<br />
21 * n and k are already equal, so no changes are needed.
22 * </div>
23 * <strong class="example">Example 3:
24 * <div class="example-block">
25 * Input: <span class="example-io">n = 14, k = 13</span>
26 * Output: <span class="example-io">-1</span>
27 * Explanation:<br />
28 * It is not possible to make n equal to k.
29 * </div>
30 *  
31 * Constraints:
32 * 
33 * 	1 <= n, k <= 10^6
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/
39// discuss: https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn min_changes(n: i32, k: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3226() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.