Given a number N, the task is to find the count of X such that N XOR X == N OR X, where 0<=X<=N
Input: N = 5
For N = 5,
5 XOR 2 == 5 OR 2
5 XOR 0 == 5 OR 0
Thus, count is 2.
Input: N = 7
For N = 7,
7 XOR 0 == 7 OR 0
Thus, count is 1.
Approach: The idea is to convert given number to binary and then count the unset bits in it. 2^count gives us the number of X such that N XOR X == N OR X.
Below is the implementation of the above approach:
$bit = $N % 2;
if ($bit == 0)
$N = intval($N / 2);
return pow(2, $count);
// Driver code
$N = 7;
// This code is contributed
// by ChitraNayal
- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose difference with N is equal to XOR with N
- Count the numbers < N which have equal number of divisors as K
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count pairs of natural numbers with GCD equal to given number
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Maximum count of equal numbers in an array after performing given operations
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count pairs from two arrays having sum equal to K
- Count minimum bits to flip such that XOR of A and B equal to C
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count pairs in an array such that both elements has equal set bits
- Numbers whose bitwise OR and sum with N are equal
- Split N^2 numbers into N groups of equal sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.