Maximizing Unset Bits in Integer with Constraints
Last Updated :
27 Dec, 2023
Given a non-negative integer n. You are only allowed to make a set bit unset, the task is to find the maximum possible value of the query so that after performing the given operations, no three consecutive bits of the integer query 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 perfrom the operation of changing set bit to unset bit. Now, the number becomes 6 that is …..00110. It satifies 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.
Step-by-step approach:
- Covert the given query into binary form and store it in the array named set.
- Keep an answer variable 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.
- And also take the bitwiseOR operation with answer and 2i , to add the all set bit values.
- Return the answer.
Below is the implementation of 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 input = 6;
int result = noConseBits(input);
cout << "Number with no consecutive set bits: "
<< result << endl;
return 0;
}
|
Java
public class Main {
static int noConsecutiveBits( 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 input = 6 ;
int result = noConsecutiveBits(input);
System.out.println(
"Number with no consecutive set bits: "
+ result);
}
}
|
Python
def no_consecutive_bits(n):
bit_set = [ 0 ] * 35
for j in range ( 30 , - 1 , - 1 ):
if ( 1 << j) & n:
bit_set[j] = 1
fin_ans = 0
for j in range ( 30 , 1 , - 1 ):
if bit_set[j] = = 1 :
fin_ans | = ( 1 << j)
if bit_set[j - 1 ] = = 1 :
bit_set[j - 2 ] = 0
if bit_set[ 1 ] = = 1 :
fin_ans | = 2
if bit_set[ 0 ] = = 1 :
fin_ans | = 1
return fin_ans
if __name__ = = "__main__" :
input_value = 6
result = no_consecutive_bits(input_value)
print ( "Number with no consecutive set bits:" , result)
|
C#
using System;
class Program
{
static int NoConsecutiveBits( 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 void Main()
{
int input = 6;
int result = NoConsecutiveBits(input);
Console.WriteLine( "Number with no consecutive set bits: " + result);
}
}
|
Javascript
function noConsecutiveBits(n) {
let set = Array(35).fill(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 input = 6;
let result = noConsecutiveBits(input);
console.log( "Number with no consecutive set bits: " + result);
|
Output
Number with no consecutive set bits: 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 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...