LeetCode in Kotlin

3827. Count Monobit Integers

Easy

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:

Example 2:

Input: n = 4

Output: 3

Explanation:

Constraints:

Solution

class Solution {
    fun countMonobit(n: Int): Int {
        var count = 1
        var `val` = 1
        while (`val` <= n) {
            count++
            `val` = (`val` shl 1) or 1
        }
        return count
    }
}