-
Notifications
You must be signed in to change notification settings - Fork 97
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ignacio-chiazzo
merged 5 commits into
ignacio-chiazzo:master
from
chhavibansal:hackoct_24
Oct 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae6cb62
Leetcode problem 3226
2bf1ed1
Apply suggestions from code review
chhavibansal 822c709
Update Bit_Reverse_To_Make_Numbers_Equal.js
chhavibansal 3df5f0e
Leetcode problem 3226
0a1963b
Merge branch 'master' into hackoct_24
ignacio-chiazzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
LeetcodeProblems/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,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; | ||
} | ||
20 changes: 20 additions & 0 deletions
20
LeetcodeProblemsTests/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.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.