Description

Given a positive integer n, find the longest distance between any two adjacent 1s in the binary representation of n. If there are no two adjacent 1s, return 0.

Examples

Input:n = 22
Output:2
Explanation:

22 is 10110, gap of 2.

Input:n = 8
Output:0
Explanation:

8 is 1000 in binary. There is only one '1' bit, so there is no gap between consecutive 1-bits.

Input:n = 529
Output:4
Explanation:

529 is 1000010001 in binary. There are two gaps: one of length 4 between the first and second 1, and another of length 3 between the second and third 1. The longest gap is 4.

Constraints

  • 1 ≤ n ≤ 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!