Count numbers less than N whose Bitwise AND with N is zero
Given a positive integer N, the task is to count all numbers which are less than N, whose Bitwise AND of all such numbers with N is zero.
Examples:
Input: N = 5
Output: 2
Explanation: The integers less than N(= 5) whose Bitwise AND with 5 is 0 are 0 and 2. Hence, the total count is 2.
Input: N = 9
Output: 4
Naive approach: The idea is to go for every number less than n and check if it’s Bitwise AND with n is zero (0) or not. If bitwise AND becomes zero then increment the counter and finally return the counter.
Follow the steps below to implement the above idea:
- Initialize count with 0
- Iterate from i = 0 to n and calculate the bitwise AND of i with n
If bitwise AND with n becomes zero then increment the value of count by 1.
- Return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countBitwiseZero( int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int temp = n & i;
if (temp == 0) {
count++;
}
}
return count;
}
int main()
{
int n = 9;
cout << countBitwiseZero(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countBitwiseZero( int n){
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int temp = n & i;
if (temp == 0 ) {
count++;
}
}
return count;
}
public static void main(String args[])
{
int n = 9 ;
int ans = countBitwiseZero(n);
System.out.print(ans);
}
}
|
Python3
def countBitwiseZero(n):
count = 0
for i in range (n):
temp = n & i
if temp = = 0 :
count + = 1
return count
n = 9
print (countBitwiseZero(n))
|
C#
using System;
class GFG {
static int CountBitwiseZero( int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int temp = n & i;
if (temp == 0) {
count++;
}
}
return count;
}
static void Main( string [] args)
{
int n = 9;
int ans = CountBitwiseZero(n);
Console.WriteLine(ans);
}
}
|
Javascript
function countBitwiseZero(n) {
let count = 0;
for (let i = 0; i < n; i++) {
let temp = n & i;
if (temp == 0) {
count++;
}
}
return count;
}
let n = 9;
console.log(countBitwiseZero(n));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach: The given problem can be solved based on the observation that all bits which are set in N will be unset in any number which has Bitwise AND with N equal to 0. Follow the steps below to solve the problem:
- Initialize a variable, say unsetBits, equal to the total number of unset bits in the given integer N.
- Now, every unset bit in N can have either 0 or 1 in the corresponding position, as the Bitwise AND for any position where N has an unset bit will always be equal to 0. Hence, the total number of different possibilities will be 2 raised to the power of unsetBits.
- Therefore, print the value of 2 to the power of unsetBits as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countUnsetBits( int N)
{
int c = 0;
while (N) {
if (N % 2 == 0) {
c += 1;
}
N = N >> 1;
}
return c;
}
void countBitwiseZero( int N)
{
int unsetBits = countUnsetBits(N);
cout << (1 << unsetBits);
}
int main()
{
int N = 9;
countBitwiseZero(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countUnsetBits( int N)
{
int c = 0 ;
while (N != 0 ) {
if (N % 2 == 0 ) {
c += 1 ;
}
N = N >> 1 ;
}
return c;
}
static void countBitwiseZero( int N)
{
int unsetBits = countUnsetBits(N);
System.out.print( 1 << unsetBits);
}
public static void main(String[] args)
{
int N = 9 ;
countBitwiseZero(N);
}
}
|
Python3
def countUnsetBits(N):
c = 0
while (N):
if (N % 2 = = 0 ):
c + = 1
N = N >> 1
return c
def countBitwiseZero(N):
unsetBits = countUnsetBits(N)
print (( 1 << unsetBits))
if __name__ = = '__main__' :
N = 9
countBitwiseZero(N)
|
C#
using System;
class GFG{
static int countUnsetBits( int N)
{
int c = 0;
while (N != 0)
{
if (N % 2 == 0)
{
c += 1;
}
N = N >> 1;
}
return c;
}
static void countBitwiseZero( int N)
{
int unsetBits = countUnsetBits(N);
Console.Write(1 << unsetBits);
}
public static void Main(String[] args)
{
int N = 9;
countBitwiseZero(N);
}
}
|
Javascript
<script>
function countUnsetBits( N)
{
let c = 0;
while (N != 0) {
if (N % 2 == 0) {
c += 1;
}
N = N >> 1;
}
return c;
}
function countBitwiseZero( N)
{
let unsetBits = countUnsetBits(N);
document.write(1 << unsetBits);
}
let N = 9;
countBitwiseZero(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
06 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...