Numbers whose bitwise OR and sum with N are equal
Last Updated :
12 Jun, 2022
Given a non-negative integer N, the task is to find count of non-negative integers
less than or equal to N whose bitwise OR and sum with N are equal.
Examples :
Input : N = 3
Output : 1
0 is the only number in [0, 3]
that satisfies given property.
(0 + 3) = (0 | 3)
Input : 10
Output : 4
(0 + 10) = (0 | 10) (Both are 10)
(1 + 10) = (1 | 10) (Both are 11)
(4 + 10) = (4 | 10) (Both are 14)
(5 + 10) = (5 | 10) (Both are 15)
A simple solution is to traverse all numbers from 0 to N and do bitwise OR and SUM with N, if both are equal increment counter.
Time complexity = O(N).
An efficient solution is to follow following steps.
1. Find count of zero bit in N.
2. Return pow(2,count).
The idea is based on the fact that bitwise OR and sum of a number x with N are equal, if and only if
bitwise AND of x with N will is 0
Let, N=10 =10102.
Bitwise AND of a number with N will be 0, if number
contains zero bit with all respective set bit(s) of
N and either zero bit or set bit with all respective
zero bit(s) of N (because, 0&0=1&0=0).
e.g.
bit : 1 0 1 0
position: 4 3 2 1
Bitwise AND of any number with N will be 0, if the number
has following bit pattern
1st position can be either 0 or 1 (2 ways)
2nd position can be 1 (1 way)
3rd position can be either 0 or 1 (2 ways)
4th position can be 1 (1 way)
Total count = 2*1*2*1 = 22 = 4.
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int CountZeroBit( int n)
{
unsigned int count = 0;
while (n)
{
if (!(n & 1))
count++;
n >>= 1;
}
return count;
}
int CountORandSumEqual( int N )
{
int count = CountZeroBit(N);
return (1 << count);
}
int main()
{
int N = 10;
cout << CountORandSumEqual(N);
return 0;
}
|
Java
class GFG {
static int CountZeroBit( int n)
{
int count = 0 ;
while (n > 0 )
{
if ((n & 1 ) != 0 )
count++;
n >>= 1 ;
}
return count;
}
static int CountORandSumEqual( int N )
{
int count = CountZeroBit(N);
return ( 1 << count);
}
public static void main (String[] args)
{
int N = 10 ;
System.out.print(CountORandSumEqual(N));
}
}
|
Python3
def CountZeroBit(n):
count = 0
while (n):
if ( not (n & 1 )):
count + = 1
n >> = 1
return count
def CountORandSumEqual(N):
count = CountZeroBit(N)
return ( 1 << count)
N = 10
print (CountORandSumEqual(N))
|
C#
using System;
class GFG
{
static int CountZeroBit( int n)
{
int count = 0;
while (n>0)
{
if (n%2!=0)
count++;
n >>= 1;
}
return count;
}
static int CountORandSumEqual( int N )
{
int count = CountZeroBit(N);
return (1 << count);
}
public static void Main()
{
int N = 10;
Console.Write(CountORandSumEqual(N));
}
}
|
PHP
<?php
function CountZeroBit( $n )
{
$count = 0;
while ( $n )
{
if (!( $n & 1))
$count ++;
$n >>= 1;
}
return $count ;
}
function CountORandSumEqual( $N )
{
$count = CountZeroBit( $N );
return (1 << $count );
}
$N = 10;
echo CountORandSumEqual( $N );
?>
|
Javascript
<script>
function CountZeroBit(n)
{
let count = 0;
while (n>0)
{
if (n%2!=0)
count++;
n >>= 1;
}
return count;
}
function CountORandSumEqual(N)
{
let count = CountZeroBit(N);
return (1 << count);
}
let N = 10;
document.write(CountORandSumEqual(N));
</script>
|
Output :
4
Total time complexity: O(log2(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...