Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 7 Mar, 2021

Easy

```
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:

- First, count the total number of zeros and ones present in the binary representation of the number ‘N’.
- If the total number of zeros is 0 then the longest consecutive number of ones present in the representation is the total length of the binary number.
- If the total number of ones is 0 then the longest consecutive number of ones present in the representation is 1 (since you can get at most a single one by flipping any of the zeros once).

- Now iterate over the length of the binary representation of the number (length N) and check:
- For every 0, count the number of 1s on both sides of it.
- Start from the position of zero bit and keep moving towards left until another zero bit is reached or the end of the number is reached, keep incrementing the count of the number of ones present on the left side of the zero bit.
- Now start moving towards the right until another zero bit is reached or the end of the number is reached, keep incrementing the count of the number of ones present on the right side of zero bit.
- Add both of the counts and also an extra one (since the zero bit which would be flipped should also be counted).

- Check for every zero bit encountered if the present count of consecutive ones is greater than the maximum count of consecutive ones obtained till now, update the
- ‘MAXIMUMCONSECUTIVEONECOUNT = ’PRESENTCOUNT , if it is greater.
- We finally return ‘MAXIMUMCONSECUTIVEONECOUNT’.

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:

- For each bit ‘i’ from 0 to log('N'):
- If the current bit is a ‘1’ then,
- update the previous Length 'PREVLEN' by setting it equal to the current Length ‘CURRLEN’.

- If the next bit is a 0, then
- We can’t merge these sequences together, so we set the previous Length 'PREVLEN' equal to 0.

- Now, We update the maximum length ‘MAXLEN’ as -
- ‘MAXLEN’ = max(MAXLEN, CURRLEN + PREVLEN).

- We finally return ‘MAXLEN’.