LeetCode Streak Day 14: Binary Concatenation Solution

Day 14/30 – LeetCode streak Today’s problem: Concatenation of Consecutive Binary Numbers  You need the decimal value of '1' + '2' + '3' + ... + 'n' written in binary back-to-back, all under mod (10^9 + 7). Core trick: treat “concatenate in binary” as shift + OR: * When you append 'i' to the right, you’re really shifting the current result left by 'bits(i)' and OR-ing 'i' into the free space. * The number of bits only increases when 'i' hits a power of two (1, 2, 4, 8, …), so you just track 'bits' and bump it whenever '(i & (i - 1)) == 0'. Day 14 takeaway: Once you see that “stick this binary to the right” is the same as “shift by its bit length and OR”, the whole problem becomes a clean for-loop plus the power-of-two trick—no string building or big integer juggling needed. #leetcode #dsa #java #bitmanipulation #consistency

  • graphical user interface, application

Good explanation. Using bit shifts instead of building strings makes the solution much more efficient. These small bit manipulation tricks make a big difference in performance.

To view or add a comment, sign in

Explore content categories