class Solution:
def concatenatedBinary(self, n: int) -> int:
MOD = 10**9 + 7
result = 0
length = 0
for i in range(1, n + 1):
# If i is a power of 2, increase the bit length
if (i & (i - 1)) == 0:
length += 1
result = ((result << length) | i) % MOD
return result
class Solution {
public:
int concatenatedBinary(int n) {
const int MOD = 1e9 + 7;
long result = 0;
int length = 0;
for (int i = 1; i <= n; ++i) {
if ((i & (i - 1)) == 0) {
++length;
}
result = ((result << length) | i) % MOD;
}
return (int)result;
}
};
class Solution {
public int concatenatedBinary(int n) {
final int MOD = 1_000_000_007;
long result = 0;
int length = 0;
for (int i = 1; i <= n; ++i) {
if ((i & (i - 1)) == 0) {
++length;
}
result = ((result << length) | i) % MOD;
}
return (int) result;
}
}
var concatenatedBinary = function(n) {
const MOD = 1e9 + 7;
let result = 0;
let length = 0;
for (let i = 1; i <= n; ++i) {
if ((i & (i - 1)) === 0) {
++length;
}
result = ((result << length) | i) % MOD;
}
return result;
};
Given a positive integer n
, you are to concatenate the binary representations of all numbers from 1
to n
in order, and interpret the resulting binary string as a decimal number. Return this number modulo 10^9 + 7
.
For example, if n = 3
:
1
), 2 (10
), 3 (11
)11011
27
27
1 <= n <= 105
10^9 + 7
The naive approach is to convert each number from 1
to n
to its binary string, concatenate all these strings, and then convert the final string to an integer. However, this approach is not efficient for large n
because the concatenated string can be extremely large (up to hundreds of thousands of bits).
To optimize, we need to avoid building huge strings and instead work directly with numbers and bit operations. The key insight is that concatenating a new number's binary representation to the right of an existing binary number is equivalent to shifting the existing number left by the length of the new binary, and then adding the new number.
We also need to keep the result modulo 10^9 + 7
at every step to prevent integer overflow.
result
to 0
to keep the ongoing concatenated value.1
to n
:i
, determine the length of its binary representation. This can be done by checking if i
is a power of two (since the number of bits increases by 1 only at powers of two).result
left by the number of bits in i
(equivalent to multiplying by 2^{length}
), then add i
.10^9 + 7
at each step.result
.Using bit manipulation and modular arithmetic allows us to efficiently compute the answer without ever building a full binary string.
Let's walk through the process for n = 3
:
i = 1
1
(1 bit)0
1
i = 2
10
(2 bits, because 2 is a power of two)100
(binary) = 4 (decimal)110
(binary) = 6 (decimal)i = 3
11
(still 2 bits)11000
(binary) = 24 (decimal)11011
(binary) = 27 (decimal)27
.
By recognizing that concatenating binary numbers is equivalent to shifting and OR-ing, we avoid expensive string operations and keep the computation efficient. The key insight is to update the bit length only when we hit a power of two, and to use modular arithmetic at every step. This makes the solution both elegant and highly efficient, scaling easily to large values of n
.