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