The Wayback Machine - https://web.archive.org/web/20230330214946/https://www.geeksforgeeks.org/maximum-value-with-no-3-consecutive-set-bits/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Maximum value with no 3 consecutive set bits

Improve Article
Save Article
Like Article
  • Last Updated : 29 Mar, 2023
Improve Article
Save Article
Like Article

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.
  • Covert 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.
  • Return the answer.

Below is the code for the above approach:

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
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)

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.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!