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++
// 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;
}
Java
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
# 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)
C#
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);
}
}
JavaScript
// 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);
OutputNumber 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.
Similar Reads
Computing INT_MAX and INT_MIN with Bitwise operations Prerequisites : INT_MAX and INT_MIN in C/C++ and Applications. Arithmetic shift vs Logical shiftSuppose you have a 32-bit system : The INT_MAX would be 01111111111111111111111111111111 and INT_MIN would be 10000000000000000000000000000000. 0 & 1 in most-significant bit position representing the
4 min read
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 = 2Output: 2Explanation: 2's binary form is 10, no 3 consecutive set bits are here. So, 2 itself would b
6 min read
Maximize given integer by swapping pairs of unequal bits Given a positive integer N, the task is to determine the maximum possible integer that can be formed by performing the following operations on the given integer N: Convert the integer into its binary representation.Swap only unequal bits in its binary representation. Examples: Input : 11Output : 14E
7 min read
Find closest integer with the same weight Given a positive integer X the task is to find an integer Y such that: The count of set bits is Y is equal to the count of set bits in X.X != Y.|X - Y| is minimum. Examples: Input: X = 92 Output: 90 90 is the closest number to 92 having equal number of set bits.Input: X = 17 Output: 18 Approach: A l
5 min read
Find element with the maximum set bits in an array Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.Examples: Input: arr[] = {10, 100, 1000, 10000} Output: 1000 Binary(10) = 1010 (2 set bits) Binary(100) = 1100100 (3 set bits) Binary(1000) = 1111101000 (6 set bits) Binary(10000) = 10011100010000
5 min read
Set bits in N equals to M in the given range. You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).Examples : Input : N = 1, M = 2, i = 2, j = 4Output: 9N = 00000001(Considering 8 bits only)M = 1
7 min read
Print first n numbers with exactly two set bits Given a number n, print first n positive integers with exactly two set bits in their binary representation.Examples : Input: n = 3Output: 3 5 6The first 3 numbers with two set bits are 3 (0011),5 (0101) and 6 (0110)Input: n = 5Output: 3 5 6 9 10 12A Simple Solution is to consider all positive intege
10 min read
Unset bits in the given range Given a non-negative number n and two values l and r. The problem is to unset the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost rth bit.Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
8 min read
Maximize total set bits of elements in N sized Array with sum M Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1,
8 min read
Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
5 min read