Big Binary Numbers:
Given a number N, find the decimal representation by merging binary equivalent [1..N].
Input N is Long number, Mod output by 10 ^ 9 + 7
Example:
N = 3
1 = 1
2 = 10
3 = 11
Merged binary nums: 11011
Output: 27
Solution[Java]:
private static long solve( long n ) {
StringBuilder sb = new StringBuilder();
for (long i = 1; i <= n; i++) {
sb.append(Long.toBinaryString(i));
}
Long res = new BigInteger(sb.toString(), 2).longValue();
return (res + (long) (Math.pow(10, 9) + 7)) % (long) (Math.pow(10, 9) + 7);
}The above solution is passing only few test cases. Any suggestion, how to write an optimized version?
I am really demotivated after attempting this question. I solved ~400 leetcode problems but stands nowhere in the programming community :(