Tuesday, June 6, 2023
HomeSoftware DevelopmentMost worth with no 3 consecutive set bits

# Most worth with no 3 consecutive set bits

Given a non-negative 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.
• 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 (i-1)th bit is ready then unset the (i-2)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.
• And in addition take the bitwiseOR operation with reply and a couple ofi, so as to add the all set bit values.
• if (set[1] == 1), fin_ans |= 2.
• if (set[0] == 1), fin_ans |= 1.

Beneath is the code for the above strategy:

## C++

 `#embrace ` `utilizing` `namespace` `std;` ` `  `int` `noConseBits(``int` `n)` `{` `    ``int` `set[35];` ` `  `    ``for` `(``int` `j = 0; j < 35; j++)` `        ``set[j] = 0;` ` `  `    ``for` `(``int` `j = 30; j >= 0; j--) {` `        ``if` `((1 << j) & n) {` `            ``set[j] = 1;` `        ``}` `    ``}` `    ``int` `fin_ans = 0;` `    ``for` `(``int` `j = 30; j >= 2; j--) {` `        ``if` `(set[j] == 1) {` `            ``fin_ans |= (1 << j);` `            ``if` `(set[j - 1] == 1) {` `                ``set[j - 2] = 0;` `            ``}` `        ``}` `    ``}` `    ``if` `(set[1] == 1)` `        ``fin_ans |= 2;` `    ``if` `(set[0] == 1)` `        ``fin_ans |= 1;` ` `  `    ``return` `fin_ans;` `}` ` `  `int` `primary()` `{` `    ``int` `n = 7;` `    ``int` `ans = noConseBits(n);` ` `  `    ` `    ``cout << ans;` `    ``return` `0;` `}`

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.

RELATED ARTICLES