Leetcode 1800 Solution

This article provides solution to leetcode question 1800 (concatenation-of-consecutive-binary-numbers)

https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers

Solution

class Solution:
    def concatenatedBinary(self, n: int) -> int:
        if n == 1:
            return 1

        curr = 1
        blen = 0
        v = 1
        for i in range(2, n + 1):
            while v <= i:
                blen += 1
                v *= 2

            curr = ((curr << blen) + i) % 1000000007

        return curr