Leetcode 1680: Concatenation of Consecutive Binary Numbers

grid47
grid47
Exploring patterns and algorithms
May 23, 2024 4 min read

Given an integer n, form a binary string by concatenating the binary representations of all integers from 1 to n. Convert this concatenated binary string to its decimal equivalent and return the result modulo 10^9 + 7.
Problem
Approach
Steps
Complexity
Input: The input consists of a single integer `n`.
Example: n = 3
Constraints:
• 1 <= n <= 10^5
Output: Return the decimal value of the concatenated binary string modulo (10^9 + 7).
Example: Output: 27
Constraints:
Goal: The goal is to compute the decimal value of the concatenated binary representations of integers from 1 to `n` and return the result modulo (10^9 + 7).
Steps:
• Initialize a variable to store the result.
• Iterate from 1 to `n`, converting each number to its binary form.
• Concatenate the binary representations.
• Convert the concatenated binary string into its decimal form.
• Return the result modulo (10^9 + 7).
Goal: The input integer `n` satisfies the following constraints:
Steps:
• 1 <= n <= 10^5
Assumptions:
• The input integer `n` is valid and within the specified range.
Input: Input: n = 3
Explanation: The binary representations of integers 1, 2, and 3 are concatenated as '11011', which corresponds to the decimal value 27.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus