Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 96a6a65

Browse files
committed
Add solution #2429
1 parent 4ea5752 commit 96a6a65

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

2429-minimize-xor.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2429. Minimize XOR
3+
* https://leetcode.com/problems/minimize-xor/
4+
* Difficulty: Medium
5+
*
6+
* Given two positive integers num1 and num2, find the positive integer x such that:
7+
* - x has the same number of set bits as num2, and
8+
* - The value x XOR num1 is minimal.
9+
*
10+
* Note that XOR is the bitwise XOR operation.
11+
*
12+
* Return the integer x. The test cases are generated such that x is uniquely determined.
13+
*
14+
* The number of set bits of an integer is the number of 1's in its binary representation.
15+
*/
16+
17+
/**
18+
* @param {number} num1
19+
* @param {number} num2
20+
* @return {number}
21+
*/
22+
var minimizeXor = function(num1, num2) {
23+
let count1 = num1.toString(2).split('1').length - 1;
24+
const count2 = num2.toString(2).split('1').length - 1;
25+
26+
while (count1 > count2) {
27+
num1 &= (num1 - 1);
28+
count1--;
29+
}
30+
31+
while (count1 < count2) {
32+
num1 |= (num1 + 1);
33+
count1++;
34+
}
35+
36+
return num1;
37+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@
412412
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
413413
2413|[Smallest Even Multiple](./2413-smallest-even-multiple.js)|Easy|
414414
2427|[Number of Common Factors](./2427-number-of-common-factors.js)|Easy|
415+
2429|[Minimize XOR](./2429-minimize-xor.js)|Medium|
415416
2469|[Convert the Temperature](./2469-convert-the-temperature.js)|Easy|
416417
2482|[Difference Between Ones and Zeros in Row and Column](./2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
417418
2490|[Circular Sentence](./2490-circular-sentence.js)|Easy|

0 commit comments

Comments
 (0)