Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on bit manipulation fundamentals.
You are given two positive integers n and k.
You can choose any bit in the binary representation of n that is equal to 1 and change it to 0.
Return the number of changes needed to make n equal to k. If it is impossible, return -1.
Example 1:
Input: n = 13, k = 4
Output: 2
Explanation:
Initially, the binary representations of n and k are n = (1101)2 and k = (0100)2.
We can change the first and fourth bits of n. The resulting integer is n = (0100)2 = k.
Example 2:
Input: n = 21, k = 21
Output: 0
Explanation:
n and k are already equal, so no changes are needed.
Example 3:
Input: n = 14, k = 13
Output: -1
Explanation:
It is not possible to make n equal to k.
Constraints:
1 <= n, k <= 106Problem summary: You are given two positive integers n and k. You can choose any bit in the binary representation of n that is equal to 1 and change it to 0. Return the number of changes needed to make n equal to k. If it is impossible, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Bit Manipulation
13 4
21 21
14 13
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3226: Number of Bit Changes to Make Two Integers Equal
class Solution {
public int minChanges(int n, int k) {
return (n & k) != k ? -1 : Integer.bitCount(n ^ k);
}
}
// Accepted solution for LeetCode #3226: Number of Bit Changes to Make Two Integers Equal
func minChanges(n int, k int) int {
if n&k != k {
return -1
}
return bits.OnesCount(uint(n ^ k))
}
# Accepted solution for LeetCode #3226: Number of Bit Changes to Make Two Integers Equal
class Solution:
def minChanges(self, n: int, k: int) -> int:
return -1 if n & k != k else (n ^ k).bit_count()
// Accepted solution for LeetCode #3226: Number of Bit Changes to Make Two Integers Equal
impl Solution {
pub fn min_changes(n: i32, k: i32) -> i32 {
if (n & k) != k {
-1
} else {
(n ^ k).count_ones() as i32
}
}
}
// Accepted solution for LeetCode #3226: Number of Bit Changes to Make Two Integers Equal
function minChanges(n: number, k: number): number {
return (n & k) !== k ? -1 : bitCount(n ^ k);
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.