File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 4124122396|[ Strictly Palindromic Number] ( ./2396-strictly-palindromic-number.js ) |Medium|
4134132413|[ Smallest Even Multiple] ( ./2413-smallest-even-multiple.js ) |Easy|
4144142427|[ Number of Common Factors] ( ./2427-number-of-common-factors.js ) |Easy|
415+ 2429|[ Minimize XOR] ( ./2429-minimize-xor.js ) |Medium|
4154162469|[ Convert the Temperature] ( ./2469-convert-the-temperature.js ) |Easy|
4164172482|[ Difference Between Ones and Zeros in Row and Column] ( ./2482-difference-between-ones-and-zeros-in-row-and-column.js ) |Medium|
4174182490|[ Circular Sentence] ( ./2490-circular-sentence.js ) |Easy|
You can’t perform that action at this time.
0 commit comments