New update is available. Click here to update.

Flip Bit to Win

Posted: 7 Mar, 2021
Difficulty: Easy


Try Problem

Ninjas are often known for their stealth execution and accuracy to get the job done right. While honing their art of moving through dense forests stealthily, they need the maximum number of continuous trees one after the other for practicing.

Trees are represented by 1s and empty places by 0s (basically a binary representation of a given integer). You are also given an extra tree which you can plant at any empty place (i.e. you can flip one of the zeroes in the binary representation to 1). The tree should be planted such that the maximum number of consecutive trees is maximized.

Your task is to report the maximum number of consecutive trees after plantation.


You may also choose not to plant that extra tree at all.
For Example:
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.
Input format:
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.
Output format:
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