Binary Gap Problem

Binary Gap Problem — ExecCode Easy DSA Practice

Solve the Binary Gap problem on ExecCode. Free online easy DSA practice in Bit Manipulation. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description You are given a positive integer n, find and return the longest distance between any two adjacent 1 's in the binary representation of n . Your task is to if there are no two adjacent 1 's, return 0 . Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.

Examples

Input {"n": 22}; Output 2. Input {"n": 8}; Output 0

Constraints

1 <= n <= 10^9

Practice Binary Gap free on ExecCode. Browse DSA problems, topic map, and placement guides.