class Solution:
    def binaryGap(self, n: int) -> int:
        prev = 0
        res = 0
        for i, d in enumerate(bin(n)[3:]):
            if d == "1":
                res = max(res, i-prev+1)
                prev = i + 1
        return res
class Solution {
    public int binaryGap(int n) {
      char[] arr = Integer.toBinaryString(n).toCharArray();
        List<Integer> ans = new ArrayList();
        for(int i = 0; i < arr.length ; i++){
            if(arr[i] == '1')
                ans.add(i);
        }
        int res = 0;
        for ( int i = 0 ; i < ans.size() -1 ; i++){
            res =Math.max(res,ans.get(i+1) - ans.get(i));
        }
        return res;
    }
}
Binary Gap
Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. 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.

&nbsp;
Example 1:

Input: n = 22
Output: 2
Explanation: 22 in binary is "10110".
The first adjacent pair of 1's is "10110" with a distance of 2.
The second adjacent pair of 1's is "10110" with a distance of 1.
The answer is the largest of these two distances, which is 2.
Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.


Example 2:

Input: n = 8
Output: 0
Explanation: 8 in binary is "1000".
There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.


Example 3:

Input: n = 5
Output: 2
Explanation: 5 in binary is "101".


&nbsp;
Constraints:


	1 &lt;= n &lt;= 109


var binaryGap = function(n) {
    var str = (n >>> 0).toString(2), start = 0, end = 0, diff = 0;
    for(var i=0;i<str.length;i++) {
    	if(str[i] === '1') {
      	end = i;
        diff = Math.max(diff, end - start);
        start = i;
      } 
    }
    return diff;
};
class Solution {
public:
    int binaryGap(int n) {
        int res=0;
        int s=0,i=0;
        while(n){
            if(n&1){
                s=i;break;
            }
            i++;
            n=n>>1;
        }
        while(n){
            if(n&1){
                res=max(res,(i-s));
                s=i;
            }
            i++;
            n=n>>1;
        }
        return res;
    }
};