- Notifications
You must be signed in to change notification settings - Fork98
[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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletionsLeetcodeProblems/Algorithms/easy/Bit_Reverse_To_Make_Numbers_Equal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff 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; |
20 changes: 20 additions & 0 deletionsLeetcodeProblemsTests/Algorithms/easy/Bit_Reverse_To_Make_Numbers_Equal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.