Talentd
FleetCode

3827. Count Monobit Integers

Easy66.0% acceptance1 company

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).

Example 1:

Input: n = 1

Output: 2

Explanation:

  • The integers in the range [0, 1] have binary representations "0" and "1".
  • Each representation consists of identical bits. Thus, the answer is 2.

Example 2:

Input: n = 4

Output: 3

Explanation:

  • The integers in the range [0, 4] include binaries "0", "1", "10", "11", and "100".
  • Only 0, 1 and 3 satisfy the Monobit condition. Thus, the answer is 3.

Constraints:

  • 0 <= n <= 1000

Asked at 1 company

Hints open one at a time

Hint 1
Go through all the integers in the range [0, n] and count the monobit integers
4:05
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countMonobit(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
1
Input
4
Your code is saved on this device.Solution