


You may also choose not to plant that extra tree at all.
Input: 54
Output: 5
The binary representation of 54 is 110110.
After flipping the third bit from the left, we get consecutive 5 bits. i.e. 111110.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the 'T' test cases are as follows.
The first and the only line of each test contains an integer 'N', denoting the integer whose consecutive ones in the binary representation are to be found out.
For the integer, print the length of the longest sequence of 1 s you could create by flipping exactly one bit.
Output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 1000
2 <= N <= 10^9
Where 'N' is the integer that is to be looked into for the maximum consecutive ones after flipping 1 bit.
Time limit: 1 second
Approach: The idea is to count a number of ones on both sides of each zero. The required index is the index of zero having a maximum number of ones around it. Following variables are used in implementation:
Steps:
Approach: The key observation here is that we can simply walk through the bits in the binary representation of the given number and keeping track of the longest current 1’s sequence length and the previous 1’s sequence length. We can use this information to update our answer whenever we encounter a zero.
Steps: