Maximum value with no 3 consecutive set bits
Last Updated :
25 Apr, 2023
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.
- Return the answer.
Below is the code for the above approach:
C++
#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;
}
int main()
{
int n = 7;
int ans = noConseBits(n);
cout << ans;
return 0;
}
|
Java
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;
}
public static void main(String[] args)
{
int n = 7 ;
int ans = noConseBits(n);
System.out.println(ans);
}
}
|
Python3
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
n = 7
ans = noConseBits(n)
print (ans)
|
C#
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;
}
static public void Main( string [] args)
{
int n = 7;
int ans = noConseBits(n);
Console.WriteLine(ans);
}
}
|
Javascript
function noConseBits(n) {
let set = new Array(35);
for (let j = 0; j < 35; j++)
set[j] = 0;
for (let j = 30; j >= 0; j--) {
if ((1 << j) & n) {
set[j] = 1;
}
}
let fin_ans = 0;
for (let 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;
}
let n = 7;
let ans = noConseBits(n);
console.log(ans);
|
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.
Share your thoughts in the comments
Please Login to comment...