Given an integer X. The task is to find the smallest positive number Y(> 0) such that X AND Y is zero.
Examples:
Input : X = 3
Output : 4
4 is the smallest positive number whose bitwise AND with 3 is zero
Input : X = 10
Output : 1
Approach :
There are 2 cases :
- If the binary representation of X contains all 1s, in that case, all the bits of Y should be 0 to make the result of AND operation is zero. Then X+1 is our answer which is the first positive integer.
- If the binary representation of X doesn’t contain all 1s, in that case, find the first position in X at which bit is 0. Then our answer will be power(2, position)
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int findSmallestNonZeroY( int A_num)
{
string A_binary = bitset<8>(A_num).to_string();
int B = 1;
int length = A_binary.size();
int no_ones = __builtin_popcount(A_num);
if (length == no_ones )
return A_num + 1;
for ( int i=0;i<length;i++)
{
char ch = A_binary[length - i - 1];
if (ch == '0' )
{
B = pow (2.0, i);
break ;
}
}
return B;
}
int main()
{
int X = findSmallestNonZeroY(10);
cout << X;
}
|
Java
import java.lang.*;
public class Main {
static long findSmallestNonZeroY( long A_num)
{
String A_binary = Long.toBinaryString(A_num);
long B = 1 ;
int len = A_binary.length();
int no_ones = Long.bitCount(A_num);
if (len == no_ones) {
return A_num + 1 ;
}
for ( int i = 0 ; i < len; i++) {
char ch = A_binary.charAt(len - i - 1 );
if (ch == '0' ) {
B = ( long )Math.pow( 2.0 , ( double )i);
break ;
}
}
return B;
}
public static void main(String[] args)
{
long X = findSmallestNonZeroY( 10 );
System.out.println(X);
}
}
|
Python3
def findSmallestNonZeroY(A_num) :
A_binary = bin (A_num)
B = 1
length = len (A_binary);
no_ones = (A_binary).count( '1' );
if length = = no_ones :
return A_num + 1 ;
for i in range (length) :
ch = A_binary[length - i - 1 ];
if (ch = = '0' ) :
B = pow ( 2.0 , i);
break ;
return B;
if __name__ = = "__main__" :
X = findSmallestNonZeroY( 10 );
print (X)
|
C#
using System;
class GFG
{
static long findSmallestNonZeroY( long A_num)
{
String A_binary = Convert.ToString(A_num, 2);
long B = 1;
int len = A_binary.Length;
int no_ones = bitCount(A_num);
if (len == no_ones)
{
return A_num + 1;
}
for ( int i = 0; i < len; i++)
{
char ch = A_binary[len - i - 1];
if (ch == '0' )
{
B = ( long )Math.Pow(2.0, ( double )i);
break ;
}
}
return B;
}
static int bitCount( long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
long X = findSmallestNonZeroY(10);
Console.WriteLine(X);
}
}
|
Javascript
<script>
function findSmallestNonZeroY(A_num)
{
let A_binary = (A_num >>> 0).toString(2);
let B = 1;
let len = A_binary.length;
let no_ones = bitCount(A_num);
if (len == no_ones) {
return A_num + 1;
}
for (let i = 0; i < len; i++) {
let ch = A_binary[len - i - 1];
if (ch == '0' ) {
B = Math.floor(Math.pow(2.0, i));
break ;
}
}
return B;
}
function bitCount(x)
{
let setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
let X = findSmallestNonZeroY(10);
document.write(X);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Aug, 2021
Like Article
Save Article