[LeetCode] Count Monobit Integers

3827. Count Monobit Integers

You are given an integer n.

An integer is called Monobit if all bits in its binary representation are the same.

Return the count of Monobit integers in the range [0, n] (inclusive).

1
2
3
4
5
6
7
8
class Solution {
public:
int countMonobit(int n) {
int res = 1;
for(int i = 1; i <= n; i = i * 2 + 1) res++;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/count-monobit-integers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.