Maximum value with no 3 consecutive set bits

Given a non-negative integer n, find the maximum value that can be obtained by unsetting any set bit, such that no three consecutive bits in the resulting integer are set bits.

Examples:

Input: n = 2
Output: 2
Explanation: 2’s binary form is 10, no 3 consecutive set bits are here. So, 2 itself would be answer.

Input: n = 7
Output: 6
Explanation: 7’s binary form is …..00111.We can observe that 3 consecutive bits are set bits. This is not allowed. So, we can perform operation of changing set bit to unset bit. Now, the number becomes 6 that is …..00110. It satisfies the given condition. Hence, the maximum possible value is 6.

Approach: To solve the problem follow the below idea:

The idea is to first convert the given query into binary form and then traverse over the binary form of the query and check if 3 consecutive bits are set(1) then convert the rightmost bit of consecutive bit as unset(0) because we want maximum value at the end, for this we have to unset least significant bit which is present at the rightmost side.

Below are the steps for the above approach:

• Initialize an array set[] to 0, of size 35 to store the binary form of the given query.
• Convert the given query into binary form and store it in the array set[].
• Run a loop from j = 30 till j â‰¥ 0,
• if ((1 << j) & n), set[j] = 1.
• Initialize a variable say fin_ans to store the answer.
• Now run a loop over the set[] array from the most significant bit towards the least significant bit.
• Check if the ith bit and (i-1)th bit is set then unset the (i-2)th bit so that 3 consecutive bit must not be set.
• if (set[j] == 1), fin_ans |= (1 << j).
• if (set[j – 1] == 1), set[j – 2] = 0.
• And also take the bitwiseOR operation with answer and 2i, to add the all set bit values.
• if (set[1] == 1), fin_ans |= 2.
• if (set[0] == 1), fin_ans |= 1.

Below is the code for the above approach:

C++

 `// C++ code for the above approach:``#include ``using` `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;``}` `// Drivers code``int` `main()``{``    ``int` `n = 7;``    ``int` `ans = noConseBits(n);` `    ``// Function Call``    ``cout << ans;``    ``return` `0;``}`

Java

 `// Java code for above approach:` `import` `java.util.*;` `class` `Main {``    ``public` `static` `int` `noConseBits(``int` `n)``    ``{``        ``int``[] set = ``new` `int``[``35``];` `        ``for` `(``int` `j = ``0``; j < ``35``; j++)``            ``set[j] = ``0``;` `        ``for` `(``int` `j = ``30``; j >= ``0``; j--) {``            ``if` `(((``1` `<< j) & n) != ``0``) {``                ``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;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``7``;``        ``int` `ans = noConseBits(n);` `        ``// Function Call``        ``System.out.println(ans);``    ``}``}``// This code is contribute by Dip Prajapat`

Python3

 `# Python code for the above approach``def` `noConseBits(n):``    ``set` `=` `[``0``] ``*` `35``    ` `    ``for` `j ``in` `range``(``30``, ``-``1``, ``-``1``):``        ``if` `((``1` `<< j) & n):``            ``set``[j] ``=` `1``    ` `    ``fin_ans ``=` `0``    ``for` `j ``in` `range``(``30``, ``1``, ``-``1``):``        ``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``    ` `# Drivers code``n ``=` `7``ans ``=` `noConseBits(n)` `# Function Call``print``(ans)`

C#

 `// C# code for above approach:``using` `System;` `public` `class` `GFG {``    ``static` `int` `noConseBits(``int` `n)``    ``{``        ``int``[] ``set` `= ``new` `int``[35];``        ``for` `(``int` `j = 0; j < 35; j++)``            ``set``[j] = 0;` `        ``for` `(``int` `j = 30; j >= 0; j--) {``            ``if` `(((1 << j) & n) != 0) {``                ``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;``    ``}` `    ``// Drivers code``    ``static` `public` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 7;` `        ``// Function Call``        ``int` `ans = noConseBits(n);` `        ``Console.WriteLine(ans);``    ``}``}``// This code is contribute by prasad264`

Javascript

 `function` `noConseBits(n) {``    ``// Create an array to store the bits of n``    ``let set = ``new` `Array(35);` `    ``// Initialize all bits to 0``    ``for` `(let j = 0; j < 35; j++)``        ``set[j] = 0;` `    ``// Store the bits of n in the array``    ``for` `(let j = 30; j >= 0; j--) {``        ``if` `((1 << j) & n) {``            ``set[j] = 1;``        ``}``    ``}` `    ``// Initialize the final answer to 0``    ``let fin_ans = 0;` `    ``// Traverse the array from left to right``    ``for` `(let j = 30; j >= 2; j--) {``        ``// If the current bit is set``        ``if` `(set[j] == 1) {``            ``// Set the corresponding bit in the final answer``            ``fin_ans |= (1 << j);` `            ``// If the previous bit is also set, clear the bit two positions to the left``            ``if` `(set[j - 1] == 1) {``                ``set[j - 2] = 0;``            ``}``        ``}``    ``}` `    ``// If the second bit is set, set the second bit in the final answer``    ``if` `(set[1] == 1)``        ``fin_ans |= 2;` `    ``// If the first bit is set, set the first bit in the final answer``    ``if` `(set[0] == 1)``        ``fin_ans |= 1;` `    ``// Return the final answer``    ``return` `fin_ans;``}` `// Driver code``let n = 7;``let ans = noConseBits(n);` `// Print the result``console.log(ans);`

Output
`6`

Time Complexity: O(1), As we are traversing over the bits of the given query, in the worst case loop runs 32 times as the integer limit is 232 so we can say that time complexity is constant.
Auxiliary Space: O(1), As we are storing the bits of the given query in an array, in the worst case it takes 32 size of the array as the integer limit is 232 so we can say that space complexity is constant.

Previous
Next