1680. Concatenation of Consecutive Binary Numbers

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1680

class Solution:
    def concatenatedBinary(self, n: int) -> int:
        mod = 10**9 + 7
        res = shift = 0
        for i in range(1, n + 1):
            if (i & (i - 1)) == 0:
                shift += 1
            res = (res << shift | i) % mod
        return res