Given a nonnegative integer n, discover the utmost worth that may be obtained by unsetting any set bit, such that no three consecutive bits within the ensuing integer are set bits.
Examples:
Enter: n = 2
Output: 2
Rationalization: 2’s binary kind is 10, no 3 consecutive set bits are right here. So, 2 itself could be reply.Enter: n = 7
Output: 6
Rationalization: 7’s binary kind is …..00111.We will observe that 3 consecutive bits are set bits. This isn’t allowed. So, we are able to carry out operation of fixing set bit to unset bit. Now, the quantity turns into 6 that’s …..00110. It satisfies the given situation. Therefore, the utmost attainable worth is 6.
Strategy: To resolve the issue observe the under thought:
The thought is to first convert the given question into binary kind after which traverse over the binary type of the question and examine if 3 consecutive bits are set(1) then convert the rightmost little bit of consecutive bit as unset(0) as a result of we wish most worth on the finish, for this now we have to unset least vital bit which is current on the rightmost aspect.
Beneath are the steps for the above strategy:
 Initialize an array set[] to 0, of dimension 35 to retailer the binary type of the given question.
 Covert the given question into binary kind and retailer it within the array set[].
 Run a loop from j = 30 until j ≥ 0,
 if ((1 << j) & n), set[j] = 1.
 Run a loop from j = 30 until j ≥ 0,
 Initialize a variable say fin_ans to retailer the reply.
 Now run a loop over the set[] array from essentially the most vital bit in direction of the least vital bit.
 Verify if the ith bit and (i1)^{th} bit is ready then unset the (i2)^{th} bit in order that 3 consecutive bit should not be set.
 if (set[j] == 1), fin_ans = (1 << j).
 if (set[j – 1] == 1), set[j – 2] = 0.
 Verify if the ith bit and (i1)^{th} bit is ready then unset the (i2)^{th} bit in order that 3 consecutive bit should not be set.
 And in addition take the bitwiseOR operation with reply and a couple of^{i}, so as to add the all set bit values.
 if (set[1] == 1), fin_ans = 2.
 if (set[0] == 1), fin_ans = 1.
 Return the reply.
Beneath is the code for the above strategy:
C++

Time Complexity: O(1), As we’re traversing over the bits of the given question, within the worst case loop runs 32 instances because the integer restrict is 232 so we are able to say that point complexity is fixed.
Auxiliary Area: O(1), As we’re storing the bits of the given question in an array, within the worst case it takes 32 dimension of the array because the integer restrict is 232 so we are able to say that area complexity is fixed.