TypeScript Binary Gap
function binaryGap(n: number): number {
const trimmed = n.toString(2).replace(/^0+|0+$/g, "");
const zeroes = trimmed.split("1");
let gap = 0;
for (const zero of zeroes) {
gap = Math.max(gap, zero.length);
}
return gap;
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.