Day 48 DSA Consistency: Binary Concatenation Problem

🚀 DSA Consistency – Day 48 Today’s problem: Concatenation of Consecutive Binary Numbers 🔹 Problem Idea For a given n, concatenate the binary representations of numbers from 1 → n and return the decimal value modulo (10^9 + 7). Example: 1 → 1 2 → 10 3 → 11 Concatenation → 11011 💡 Key Intuition Instead of converting numbers to strings, we can use bit manipulation: • When a number becomes a power of 2, its binary length increases by 1 • Left shift the current result by the number of bits needed • Append the current number using addition Formula used: res = ((res << bits) + i) % mod ⚡ Why this works Every time we encounter a power of 2, the binary representation grows by one bit. We track this using: (i & (i - 1)) == 0 which efficiently checks if i is a power of two. ⏱ Complexity • Time: O(n) • Space: O(1) Consistency compounds. 48 days of showing up and solving. 💪 #DSA #LeetCode #Java #BitManipulation #Consistency #ProblemSolving

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories