BinaryGap
Problem Description here
Approach
Section titled “Approach”What is This Problem?
Section titled “What is This Problem?”This problem asks us to find the longest sequence of consecutive zeros (0) in the binary representation of a positive integer N, where this sequence is surrounded by ones (1). This is known as a “binary gap.”
For example:
N = 9(binary:1001) → gap of length 2 (the zeros between the two 1s)N = 15(binary:1111) → no gaps (no zeros between ones)N = 20(binary:10100) → gap of length 1 (the single zero between 1 and 100)
Why This Works
Section titled “Why This Works”The key insight is that we only care about zeros that appear between ones. We can think of this as tracking:
- When we see a
1→ start a new potential gap (or end current gap) - When we see a
0→ we’re inside a gap, count it - When we see another
1after zeros → gap ends, record its length
We don’t need to convert to a string — we can work directly with bit operations using bit shifts.
Algorithm Steps
Section titled “Algorithm Steps”- Convert to binary string (or use bit operations)
- Track two variables:
currentGap: zeros counted since last1maxGap: longest gap found so far
- Iterate through each bit:
- If current bit is
1:- Update
maxGapifcurrentGapis larger - Reset
currentGapto 0 (start counting new gap)
- Update
- If current bit is
0:- Increment
currentGap(we’re inside a gap)
- Increment
- If current bit is
- Return
maxGap
Visualization
Section titled “Visualization”Let’s trace through N = 1041 (binary: 10000010001):
Binary: 1 0 0 0 0 0 1 0 0 0 1Index: 0 1 2 3 4 5 6 7 8 9 10
Step 0: currentGap = 0, maxGap = 0Step 1: Bit '1' → maxGap = 0, currentGap = 0Step 2: Bit '0' → currentGap = 1Step 3: Bit '0' → currentGap = 2Step 4: Bit '0' → currentGap = 3Step 5: Bit '0' → currentGap = 4Step 6: Bit '0' → currentGap = 5Step 7: Bit '1' → maxGap = max(0,5) = 5, currentGap = 0Step 8: Bit '0' → currentGap = 1Step 9: Bit '0' → currentGap = 2Step 10: Bit '0' → currentGap = 3Step 11: Bit '1' → maxGap = max(5,3) = 5, currentGap = 0
Result: maxGap = 5Key Pattern
Section titled “Key Pattern”function binaryGap(N: number): number { const binary = N.toString(2); let maxGap = 0; let currentGap = 0;
for (const bit of binary) { if (bit === '1') { maxGap = Math.max(maxGap, currentGap); currentGap = 0; } else { currentGap++; } }
return maxGap;}
// Alternative: Using bit operations (no string conversion)function binaryGapBitwise(N: number): number { let maxGap = 0; let currentGap = 0;
while (N > 0) { if ((N & 1) === 1) { maxGap = Math.max(maxGap, currentGap); currentGap = 0; } else { currentGap++; } N >>= 1; }
return maxGap;}When to Use This Pattern
Section titled “When to Use This Pattern”Use binary gap analysis when:
- Finding longest sequence between events in binary/bits
- Analyzing bit patterns in integers
- Problems involving consecutive elements of the same type
This pattern also applies to finding longest streak of any character between two specific markers — extendable to any sequential data where you track runs of elements.
Solution
Section titled “Solution”function solution(N: number): number { const binary = N.toString(2); let maxGap = 0; let currentGap = 0;
for (const bit of binary) { if (bit === '1') { maxGap = Math.max(maxGap, currentGap); currentGap = 0; } else { currentGap++; } }
return maxGap;}