Binary Gap

Easy · Bit Manipulation · about 10 minutes

Asked at Google, Amazon, Microsoft.

Given a positive integer n, return the longest distance between two adjacent 1 bits in its binary representation (the difference of their bit positions), or 0 if there are fewer than two 1 bits.