#C7930. Longest Binary Gap

    ID: 51856 Type: Default 1000ms 256MiB

Longest Binary Gap

Longest Binary Gap

You are given a positive integer N. The task is to find the length of the longest sequence of consecutive zeros that is surrounded by ones in the binary representation of N. This sequence is called a binary gap. Note that if there is a sequence of zeros at the end of the binary representation (i.e., not bounded by a trailing 1), it is not considered a binary gap.

For example, given N = 529, its binary representation is 1000010001. It contains two binary gaps: one of length 4 and another of length 3. The answer is the maximum gap length, which is 4.

Formally, if the binary representation of N is expressed as
\(b_k b_{k-1} \ldots b_0\),
then a binary gap is any maximal contiguous subsequence of 0s that is immediately preceded and followed by 1. Your task is to compute the maximum length of such a sequence. If no such gap exists, output 0.

inputFormat

The input consists of a single integer, read from standard input. This integer represents the number N whose binary gap should be computed.

outputFormat

Output a single integer which is the length of the longest binary gap. If no binary gap exists, output 0.## sample

529
4