Given an integer N, the task is to flip all the bits to the left of rightmost set bit and print the number generated.
Examples:
Input: N = 10
Output: 6
Explanation:
10 (1010 in binary)
flipping all bits left to rightmost set bit (index 2)
-> 6 (0110 in binary)
Input: N = 120
Output: 8
Explanation:
120 (1111000 in binary)
flipping all bits left to rightmost set bit (index 3)
-> 8 (0001000 in binary)
Naive Approach:
To solve the problem mentioned above we will follow the steps given below:
- Convert the given integer into its binary form and store each bit into an array.
- Traverse the array and break after the first occurrence of 1.
- Flip all bits to the left of that index. Convert that binary sequence into a decimal number and return it.
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach:
To optimize the above method we will try to use Bitwise operators.
- We are finding the set bit position from the rightmost side that is LSB and the total number of bits.
- XOR the given number with the number which has all set bits having total set bits equal to the total number of bits.
- We don’t want to flip the rightmost bits from a set bit. So we are again taking XOR with the number which has all set bits having total set bits equal to firstbit
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totCount;
int firstCount;
void getTotCount( int num)
{
totCount = 1;
firstCount = 1;
int temp = 1;
while ((num & temp) == 0)
{
temp = temp << 1;
totCount += 1;
}
firstCount = totCount;
temp = num >> totCount;
while (temp != 0)
{
totCount += 1;
temp = temp >> 1;
}
}
int flipBitsFromRightMostSetBit( int num)
{
getTotCount(num);
int num1 = num ^ ((1 << totCount) - 1);
num1 = num1 ^ ((1 << firstCount) - 1);
return num1;
}
int main()
{
int n = 120;
cout << flipBitsFromRightMostSetBit(n)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int totCount;
static int firstCount;
static void getTotCount( int num)
{
totCount = 1 ;
firstCount = 1 ;
int temp = 1 ;
while ((num & temp) == 0 )
{
temp = temp << 1 ;
totCount += 1 ;
}
firstCount = totCount;
temp = num >> totCount;
while (temp != 0 )
{
totCount += 1 ;
temp = temp >> 1 ;
}
}
static int flipBitsFromRightMostSetBit( int num)
{
getTotCount(num);
int num1 = num ^ (( 1 << totCount) - 1 );
num1 = num1 ^ (( 1 << firstCount) - 1 );
return num1;
}
public static void main (String[] args)
{
int n = 120 ;
System.out.println(
flipBitsFromRightMostSetBit(n));
}
}
|
Python3
def getTotCount(num):
totCount = 1
firstCount = 1
temp = 1
while ( not (num & temp)):
temp = temp << 1
totCount + = 1
firstCount = totCount
temp = num >> totCount
while (temp):
totCount + = 1
temp = temp >> 1
return totCount, firstCount
def flipBitsFromRightMostSetBit(num):
totbit, firstbit = getTotCount(num)
num1 = num ^ (( 1 << totbit) - 1 )
num1 = num1 ^ (( 1 << firstbit) - 1 )
return num1
if __name__ = = '__main__' :
n = 120
print (flipBitsFromRightMostSetBit(n))
|
C#
using System;
class GFG{
static int totCount;
static int firstCount;
static void getTotCount( int num)
{
totCount = 1;
firstCount = 1;
int temp = 1;
while ((num & temp) == 0)
{
temp = temp << 1;
totCount += 1;
}
firstCount = totCount;
temp = num >> totCount;
while (temp != 0)
{
totCount += 1;
temp = temp >> 1;
}
}
static int flipBitsFromRightMostSetBit( int num)
{
getTotCount(num);
int num1 = num ^ ((1 << totCount) - 1);
num1 = num1 ^ ((1 << firstCount) - 1);
return num1;
}
public static void Main ( string [] args)
{
int n = 120;
Console.Write(
flipBitsFromRightMostSetBit(n));
}
}
|
Javascript
<script>
let totCount;
let firstCount;
function getTotCount(num)
{
totCount = 1;
firstCount = 1;
let temp = 1;
while ((num & temp) == 0)
{
temp = temp << 1;
totCount += 1;
}
firstCount = totCount;
temp = num >> totCount;
while (temp != 0)
{
totCount += 1;
temp = temp >> 1;
}
}
function flipBitsFromRightMostSetBit(num)
{
getTotCount(num);
let num1 = num ^ ((1 << totCount) - 1);
num1 = num1 ^ ((1 << firstCount) - 1);
return num1;
}
let n = 120;
document.write(flipBitsFromRightMostSetBit(n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)