java with explaination | o(n) time | o(1) space | minimum lines | bitwise
if it is asked to make a decimal number how would concatenation work?\nfor example ->\nsuppose 1 is first number and we want to concate it with 2. it can be wri
1. if it is asked to make a decimal number how would concatenation work?\nfor example ->\nsuppose 1 is first number and we want to concate it with 2. it can be written as 1* 10+2=12\nsuppose 1 is first number and we want to concate it with 21. it can be written as 1* 100+21=121\nsuppose 1 is first number and we want to concate it with 342. it can be written as 1* 1000+341=1341\nso what we can deduce is the result of append is like = original_num* pow(10,lengthOfSecondString) + secondString.\nin case of 1 and 341. length of 341 is 3 so, res= 1* pow(10,3) + 341\n2. so we just need to run loop from 2 to n and start applying above logic to build the answer.\n3. second observation. since above example is in decimal system that is why we are multiplying by 10 power. if it is binary me need to multiply by power (2,lengthOfSecondNumber).\n4. Now question is we need to find length of number in binary format. log of a number in base 2 will give the length of number in binary format +1. just like log of a number base 10 +1 gives length in decimal format. In java log base 2 method is not available only base 10 is available. so we can use below log property. \n5. for this we could use below formula - \nMath.log2(n) = Math.log10(n)/Math.log10(2);\n6.now we have length. we can using power method to multiply firstNumber* pow(2,lengthOfSecond) or we can use left shift operator. as we know left shift by 1 is multiply by 2. and left shift of length will be multiply by 2^length.\n\n\n```\nclass Solution {\n    public int concatenatedBinary(int n) {\n        long result=1;\n        int length=0;\n        for(int i=2;i<=n;i++) {\n            length=((int)(Math.log(i)/Math.log(2)))+1;\n            result=((result<<length)+i)%1000000007;\n        }\n        return (int)result;\n    }\n}\n```
['Java']