Skip to content

Commit

Permalink
Leetcode problem 3226
Browse files Browse the repository at this point in the history
  • Loading branch information
chh committed Oct 15, 2024
1 parent 11f02cd commit ae6cb62
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
3226. Number of Bit Changes to Make Two Integers Equal
https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/description/
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)base2 and k = (0100)base2.
We can change the first and fourth bits of n. The resulting integer is n = (0100)base2 = 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 <= 10^6
*/

/*
Approach
Check if transformation is possible:
Use a bitwise AND operation to ensure all 1 bits in k are also 1 bits in n. If not, return -1.
Calculate the number of changes:
Use a bitwise XOR operation to identify differing bits between n and k.
Count the number of 1s in the result of the XOR operation.
*/

/**
* @param {number} n
* @param {number} k
* @return {number}
*/
var minChanges = function bitChanges(n, k) {
// Check if transformation is possible
if ((n & k) !== k) {
return -1;
}

// Calculate the number of changes
let changes = 0;
let diff = n ^ k;

while (diff > 0) {
changes += diff & 1;
diff >>= 1;
}

return changes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require("assert");
const bitReverseToMakeNumberEqual = require("../../../LeetcodeProblems/Algorithms/easy/Bit_Reverse_To_Make_Numbers_Equal").bitChanges;


function test() {
assert.deepEqual(
bitChanges(13, 4),
2
);
assert.deepEqual(
bitChanges(21, 21),
0
);
assert.deepEqual(
bitChanges(14, 13),
-1
);
}

module.exports.test = test;
Loading

0 comments on commit ae6cb62

Please sign in to comment.