File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1933
1933
2588|[ Count the Number of Beautiful Subarrays] ( ./solutions/2588-count-the-number-of-beautiful-subarrays.js ) |Medium|
1934
1934
2592|[ Maximize Greatness of an Array] ( ./solutions/2592-maximize-greatness-of-an-array.js ) |Medium|
1935
1935
2594|[ Minimum Time to Repair Cars] ( ./solutions/2594-minimum-time-to-repair-cars.js ) |Medium|
1936
+ 2595|[ Number of Even and Odd Bits] ( ./solutions/2595-number-of-even-and-odd-bits.js ) |Easy|
1936
1937
2615|[ Sum of Distances] ( ./solutions/2615-sum-of-distances.js ) |Medium|
1937
1938
2618|[ Check if Object Instance of Class] ( ./solutions/2618-check-if-object-instance-of-class.js ) |Medium|
1938
1939
2619|[ Array Prototype Last] ( ./solutions/2619-array-prototype-last.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2595. Number of Even and Odd Bits
3
+ * https://leetcode.com/problems/number-of-even-and-odd-bits/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are given a positive integer n.
7
+ *
8
+ * Let even denote the number of even indices in the binary representation of n with value 1.
9
+ *
10
+ * Let odd denote the number of odd indices in the binary representation of n with value 1.
11
+ *
12
+ * Note that bits are indexed from right to left in the binary representation of a number.
13
+ *
14
+ * Return the array [even, odd].
15
+ */
16
+
17
+ /**
18
+ * @param {number } n
19
+ * @return {number[] }
20
+ */
21
+ var evenOddBit = function ( n ) {
22
+ let evenCount = 0 ;
23
+ let oddCount = 0 ;
24
+ let index = 0 ;
25
+
26
+ while ( n > 0 ) {
27
+ if ( n & 1 ) {
28
+ if ( index % 2 === 0 ) evenCount ++ ;
29
+ else oddCount ++ ;
30
+ }
31
+ n >>= 1 ;
32
+ index ++ ;
33
+ }
34
+
35
+ return [ evenCount , oddCount ] ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments