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

Commit 359a648

Browse files
committed
Add solution #2595
1 parent 01567c1 commit 359a648

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,7 @@
19331933
2588|[Count the Number of Beautiful Subarrays](./solutions/2588-count-the-number-of-beautiful-subarrays.js)|Medium|
19341934
2592|[Maximize Greatness of an Array](./solutions/2592-maximize-greatness-of-an-array.js)|Medium|
19351935
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|
19361937
2615|[Sum of Distances](./solutions/2615-sum-of-distances.js)|Medium|
19371938
2618|[Check if Object Instance of Class](./solutions/2618-check-if-object-instance-of-class.js)|Medium|
19381939
2619|[Array Prototype Last](./solutions/2619-array-prototype-last.js)|Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)