LC 3827 - Count Monobit Integers

Approach Simple problem, for all 0, it will be zero, and for all 1, it will be 2^i-1, so we collect all the 2^i-1 within the range. Code class Solution { public: int countMonobit(int n) { int idx = 0; while(true) { int cur = (1 << idx) - 1; if(cur > n) break; ++idx; } return idx; } };

February 1, 2026 · 1 min