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++ code for the above approach: #include <bits/stdc++.h> using namespace std;
// Function to find the number with // no consecutive set bits int noConseBits( int n)
{ // Array to store binary representation
// of the number
int set[35];
for ( int j = 0; j < 35; j++)
// Initializing the array with 0
set[j] = 0;
// Calculating binary representation
// of the number
for ( int j = 30; j >= 0; j--) {
// Checking if the j-th bit is set
if ((1 << j) & n) {
// Setting the corresponding element
// in the array
set[j] = 1;
}
}
// Variable to store the final result
int fin_ans = 0;
// Finding the number with no
// consecutive set bits
for ( int j = 30; j >= 2; j--) {
if (set[j] == 1) {
// Setting the j-th bit in
// the result
fin_ans |= (1 << j);
if (set[j - 1] == 1) {
// Resetting the j-2-th bit
// if j-1-th bit is set
set[j - 2] = 0;
}
}
}
// Setting the last two bits based
// on the array elements
if (set[1] == 1)
fin_ans |= 2;
if (set[0] == 1)
fin_ans |= 1;
// Returning the final result
return fin_ans;
} // Drivers code int main()
{ // Test the function with an example input
int input = 6;
int result = noConseBits(input);
// Drivers code
cout << "Number with no consecutive set bits: "
<< result << endl;
return 0;
} |
public class Main {
// Function to find the number with no consecutive set
// bits
static int noConsecutiveBits( int n)
{
// Array to store binary representation of the
// number
int [] set = new int [ 35 ];
for ( int j = 0 ; j < 35 ; j++) {
// Initializing the array with 0
set[j] = 0 ;
}
// Calculating binary representation of the number
for ( int j = 30 ; j >= 0 ; j--) {
// Checking if the j-th bit is set
if ((( 1 << j) & n) != 0 ) {
// Setting the corresponding element in the
// array
set[j] = 1 ;
}
}
// Variable to store the final result
int fin_ans = 0 ;
// Finding the number with no consecutive set bits
for ( int j = 30 ; j >= 2 ; j--) {
if (set[j] == 1 ) {
// Setting the j-th bit in the result
fin_ans |= ( 1 << j);
if (set[j - 1 ] == 1 ) {
// Resetting the j-2-th bit if j-1-th
// bit is set
set[j - 2 ] = 0 ;
}
}
}
// Setting the last two bits based on the array
// elements
if (set[ 1 ] == 1 )
fin_ans |= 2 ;
if (set[ 0 ] == 1 )
fin_ans |= 1 ;
// Returning the final result
return fin_ans;
}
// Driver code
public static void main(String[] args)
{
// Test the function with an example input
int input = 6 ;
int result = noConsecutiveBits(input);
// Print the result
System.out.println(
"Number with no consecutive set bits: "
+ result);
}
} |
# Python code for the above approach # Function to find the number with # no consecutive set bits def no_consecutive_bits(n):
# Array to store binary representation
# of the number
bit_set = [ 0 ] * 35
# Calculating binary representation
# of the number
for j in range ( 30 , - 1 , - 1 ):
# Checking if the j-th bit is set
if ( 1 << j) & n:
# Setting the corresponding element
# in the array
bit_set[j] = 1
# Variable to store the final result
fin_ans = 0
# Finding the number with no
# consecutive set bits
for j in range ( 30 , 1 , - 1 ):
if bit_set[j] = = 1 :
# Setting the j-th bit in
# the result
fin_ans | = ( 1 << j)
if bit_set[j - 1 ] = = 1 :
# Resetting the j-2-th bit
# if j-1-th bit is set
bit_set[j - 2 ] = 0
# Setting the last two bits based
# on the array elements
if bit_set[ 1 ] = = 1 :
fin_ans | = 2
if bit_set[ 0 ] = = 1 :
fin_ans | = 1
# Returning the final result
return fin_ans
# Drivers code if __name__ = = "__main__" :
# Test the function with an example input
input_value = 6
result = no_consecutive_bits(input_value)
# Drivers code
print ( "Number with no consecutive set bits:" , result)
|
using System;
class Program
{ // Function to find the number with
// no consecutive set bits
static int NoConsecutiveBits( int n)
{
// Array to store binary representation
// of the number
int [] set = new int [35];
for ( int j = 0; j < 35; j++)
// Initializing the array with 0
set [j] = 0;
// Calculating binary representation
// of the number
for ( int j = 30; j >= 0; j--)
{
// Checking if the j-th bit is set
if (((1 << j) & n) != 0)
{
// Setting the corresponding element
// in the array
set [j] = 1;
}
}
// Variable to store the final result
int fin_ans = 0;
// Finding the number with no
// consecutive set bits
for ( int j = 30; j >= 2; j--)
{
if ( set [j] == 1)
{
// Setting the j-th bit in
// the result
fin_ans |= (1 << j);
if ( set [j - 1] == 1)
{
// Resetting the j-2-th bit
// if j-1-th bit is set
set [j - 2] = 0;
}
}
}
// Setting the last two bits based
// on the array elements
if ( set [1] == 1)
fin_ans |= 2;
if ( set [0] == 1)
fin_ans |= 1;
// Returning the final result
return fin_ans;
}
// Driver code
static void Main()
{
// Test the function with an example input
int input = 6;
int result = NoConsecutiveBits(input);
// Driver code
Console.WriteLine( "Number with no consecutive set bits: " + result);
}
} |
// Function to find the number with // no consecutive set bits function noConsecutiveBits(n) {
// Array to store binary representation
// of the number
let set = Array(35).fill(0);
// Calculating binary representation
// of the number
for (let j = 30; j >= 0; j--) {
// Checking if the j-th bit is set
if ((1 << j) & n) {
// Setting the corresponding element
// in the array
set[j] = 1;
}
}
// Variable to store the final result
let fin_ans = 0;
// Finding the number with no
// consecutive set bits
for (let j = 30; j >= 2; j--) {
if (set[j] == 1) {
// Setting the j-th bit in
// the result
fin_ans |= (1 << j);
if (set[j - 1] == 1) {
// Resetting the j-2-th bit
// if j-1-th bit is set
set[j - 2] = 0;
}
}
}
// Setting the last two bits based
// on the array elements
if (set[1] == 1)
fin_ans |= 2;
if (set[0] == 1)
fin_ans |= 1;
// Returning the final result
return fin_ans;
} // Drivers code // Test the function with an example input let input = 6; let result = noConsecutiveBits(input); // Drivers code console.log( "Number with no consecutive set bits: " + result);
|
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.