1680. Concatenation of Consecutive Binary Numbers
Bit manipulation, mathematics and recursion

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
Search for a command to run...
Bit manipulation, mathematics and recursion

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
No comments yet. Be the first to comment.
A simple example of using C++ switch

Two dynamic programming techniques to solve Leetcode 120. Triangle. One has space complexity O(n^2). The other is O(n).

An example of using a sliding window approach and an unordered map to track character positions

A simple C++ solution to Leetcode 1695. Maximum Erasure Value using a sliding window approach and prefix sums.

Strategies to avoid them will help you excel in your following technical interview

Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 10^9 + 7.
Input: n = 1
Output: 1
Explanation: "1" in binary corresponds to the decimal value 1.
Input: n = 3
Output: 27
Explanation: In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.
Input: n = 12
Output: 505379714
Explanation: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 10^9 + 7, the result is 505379714.
1 <= n <= 10^5.There must be some relationship between the result of n and the result of n - 1.
First, let us list some first values of n.
For n = 1: the final binary string is "1", its decimal value is 1.
For n = 2: the final binary string is "110", its decimal value is 6.
For n = 3: the final binary string is "11011", its decimal value is 27.
Look at n = 3, you can see the relationship between the decimal value of "11011" and the one of "110" (of n = 2) is:
27 = 6 * 2^2 + 3
Dec("11011") = Dec("110") * 2^num_bits("11") + Dec("11")
Result(3) = Result(2) * 2^num_bits(3) + 3.
The same equation for n = 2:
6 = 1 * 2^2 + 2
Dec("110") = Dec("1") * 2^num_bits("10") + Dec("10")
Result(2) = Result(1) * 2^num_bits(2) + 2.
In general, the recursive relationship between n and n - 1 is:
Result(n) = Result(n - 1) * 2^num_bits(n) + n.
#include <cmath>
#include <iostream>
int concatenatedBinary(int n) {
unsigned long long result = 1;
for (int i = 2; i <= n; i++) {
const int num_bits = std::log2(i) + 1;
result = ((result << num_bits) + i) % 1000000007;
}
return result;
}
int main() {
std::cout << concatenatedBinary(1) << std::endl;
std::cout << concatenatedBinary(3) << std::endl;
std::cout << concatenatedBinary(12) << std::endl;
}
Output:
1
27
505379714
Runtime: O(n).
Extra space: O(1).
Thanks for reading. Feel free to share your thought about my content and check out my FREE book “10 Classic Coding Challenges”.