LeetCode Day 13: Concatenation of Consecutive Binary Numbers

🚀 Day 13 of Consistency | LeetCode Grind Continues 💪 Today I solved “Concatenation of Consecutive Binary Numbers” on LeetCode. At first glance, the problem looks simple — just concatenate binary representations from 1 to n. But the challenge is handling the huge number efficiently and returning the result modulo 10^9 + 7. 🔎 Key Learning Today: Instead of building a massive binary string (which would be inefficient), we can simulate concatenation using bit manipulation. 💡 The trick: Appending a number in binary = left shift the current result by the number of bits in that number. Add the number. Take modulo at every step. The bit length increases only when the number becomes a power of 2 (i & (i-1) == 0). This converts a potentially expensive string problem into a clean O(n) mathematical solution. 📊 Complexity: Time → O(n) Space → O(1) Small optimizations. Big impact. 🔥 Consistency > Motivation. On to Day 14 🚀 #Day13 #LeetCode #100DaysOfCode #DSA #Java #Python #Cplusplus #CodingJourney #BitManipulation #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories