Python very easy to understand
Complexity: time complexity is O(n), space is O(1).  \n\nn=1  1\nn=2  110\nn=3  11011\nn=4  11011100\n\nLet us try to find answer to n by using the value of n-1
**Complexity: time complexity is O(n), space is O(1).  **\n\nn=1  1\nn=2  110\nn=3  11011\nn=4  11011100\n\nLet us try to find answer to n by using the value of n-1 \n\nn=3\n\nWe have to calculate the value of 3 (11011) by using the value of 2\n n=2 (3-1) \nvalue =110\n\nBinary of 3=11(length 2)\nSo we multiply the initial value by 100 (1+ 2 zeroes)\nvalue_3 = value_2 *(100) +3\n\t  110 * 100 + 11\n\t  11000 + 11\n\t  11011\n\n\nSimilarly\n\nn=4\nbinary of 4=100(lenth 3)\nSo we multiply the initial value by 1000 (1+ 3 zeroes)\nvalue_4 = value_3 *(1000) + 4\n\t 11011 * 1000  + 100\n\t 11011000  + 100\n\t 11011100\n\n```\nclass Solution:\n    def concatenatedBinary(self, n):\n        ans, M = 0, 10**9 + 7\n        for x in range(1,n+1):\n            ans= (ans*(1<<(len(bin(x))-2))+x)%M\n        return ans\n\t\t\n```\n\t\t\n\n# **Try to understand the below line:**\n **ans= (ans*(1<<(len(bin(x))-2))+x)%M**\n \n1. Since we have to return modulus of 10^9 +7\n\tans= (-------) %M\n\t\n2. We have to multiply the ans of previous value by power of 10 and have to add value of x\n\tans = ( ans*(----) + x) %M\n\t\n3. left shift of integer easily give us the value like 10,100,1000,1000,...\n\tans= ( ans*(1<< (-------)) + x) %M\n\t\n4. we have to do left shift of 1 by the binary length of x \n\tans= ( ans*(1<< (len(bin(x)) + x) %M\n\t\n5. But the binary form of any number always start with 0B or 0b. So we have to reduce the lenth by 2\n\tans= ( ans*(1<< (len(bin(x))-2)) + x) %M\n
[]