Skip to content

Commit 943674c

Browse files
authored
Add function to count monobit integers
Implement a function to count monobit integers up to n by converting numbers to binary and checking for monobit conditions.
1 parent ea20210 commit 943674c

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var countMonobit = function(n) {
6+
let arr = [];
7+
let count = 0;
8+
9+
for (let i = 0; i <= n; i++) {
10+
arr.push(i.toString(2));
11+
}
12+
13+
for (let i = 0; i < arr.length; i++) {
14+
const st = arr[i].split("");
15+
16+
for (let j=0; j<st.length; j++){
17+
if (st[0] !== st[j]){
18+
count++;
19+
break;
20+
}
21+
}
22+
}
23+
const ans = (arr.length - count);
24+
return ans;
25+
};

0 commit comments

Comments
 (0)