Given a positive integer N, the task is to find the number that can be obtained by flipping consecutive set bits starting from the LSB in the binary representation of N.
Examples:
Input: N = 39
Output: 32
Explanation:
Binary representation of (39)10 = (100111)2
After flipping all consecutive set bits starting from the LSB, the number obtained is (100000)
Binary representation of (32)10 is (100000)2
Therefore, the number obtained is 32.
Input: N = 4
Output: 4
Explanation:
Binary representation of (4)10 = (100)2
Since the LSB is not set, the number remains unchanged.
Naive Approach: The simplest approach is to find the number of consecutive set bits starting from the LSB by performing Logical AND( & ) of N with 1 until N & 1 is not 0 and keep a count of the number of set bits. Then, simply left shift N by the count of set bits. Print the number obtained as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int N)
{
int count = 0;
while ((N & 1) == 1) {
N = N >> 1;
count++;
}
return N << count;
}
int main()
{
int N = 39;
cout << findNumber(N);
return 0;
}
|
Java
class GFG{
static int findNumber( int N)
{
int count = 0 ;
while ((N & 1 ) == 1 )
{
N = N >> 1 ;
count++;
}
return N << count;
}
public static void main (String[] args)
{
int N = 39 ;
System.out.println(findNumber(N));
}
}
|
Python3
def findNumber(N):
count = 0
while ((N & 1 ) = = 1 ):
N = N >> 1
count + = 1
return N << count
if __name__ = = "__main__" :
N = 39
print (findNumber(N))
|
C#
using System;
class GFG{
static int findNumber( int N)
{
int count = 0;
while ((N & 1) == 1)
{
N = N >> 1;
count++;
}
return N << count;
}
public static void Main()
{
int N = 39;
Console.WriteLine(findNumber(N));
}
}
|
Javascript
<script>
function findNumber(N)
{
let count = 0;
while ((N & 1) == 1)
{
N = N >> 1;
count++;
}
return N << count;
}
let N = 39;
document.write(findNumber(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, find the Logical AND ( & ) of N and (N + 1). The key observation is that adding 1 to a number makes every continuous set bit from the LSB to become 0. Therefore, N & (N + 1) gives the required number.
Illustration:
N = 39, therefore (N+1)=40
? N = 39 = (100111)
? N+1 = 40 = (101000)
Performing Logical AND(&) operation:
1 0 0 1 1 1
& 1 0 1 0 0 0
----------------
1 0 0 0 0 0 ? 32
----------------
Will this always work? Add 1 to N:
1 0 0 1 1 1
+ 1
-------------
1 0 1 0 0 0
--------------
It can be clearly seen that the continuous set bits from the LSB becomes unset.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int N)
{
return N & (N + 1);
}
int main()
{
int N = 39;
cout << findNumber(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findNumber( int N)
{
return N & (N + 1 );
}
public static void main(String[] args)
{
int N = 39 ;
System.out.print(findNumber(N));
}
}
|
Python3
def findNumber(N):
return N & (N + 1 )
if __name__ = = '__main__' :
N = 39
print (findNumber(N))
|
C#
using System;
class GFG
{
static int findNumber( int N)
{
return N & (N + 1);
}
public static void Main(String[] args)
{
int N = 39;
Console.Write(findNumber(N));
}
}
|
Javascript
<script>
function findNumber(N)
{
return N & (N + 1);
}
let N = 39;
document.write(findNumber(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)