Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3226] Number of Bit Changes to Make Two Integers Equal#97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
/*
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;
};

module.exports.minChanges = minChanges;
View file
Open in desktop
Original file line numberDiff line numberDiff 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
Loading

[8]ページ先頭

©2009-2025 Movatter.jp