Given a number N. The task is to count the all possible values of x such that nx is equal to (N-x), where denotes bitwise XOR operation.
Input: N = 3 Output: 4 The all possible values of x are respectively 0, 1, 2, 3. Input: N = 6 Output: 4 The all possible values of x are respectively 0, 2, 4, 6.
Approach: The XOR value of two bits will be 1 if both bits have opposite sign, and 0 when both bits are same. So on the basis of the property of XOR, we can say that n x is always greater than or equal to n-x. The only condition when its value is equal with n-x is bits of x form a subset of bits of n. Because if in the i’th position both x and n has set bits then after xor the value will decrease, and the decreased value will be , where i is 0-based position.
So the answer is the total count of subsets of bits of number n is , where k is the count of set bits in n.
Below is the implementation of above approach:
Time Complexity: O(k), where k is number of set bits in N.
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count numbers whose XOR with N is equal to OR with N
- Count numbers whose sum with x is equal to XOR with x
- Count different numbers that can be generated such that there digits sum is equal to 'n'
- Count the numbers < N which have equal number of divisors as K
- 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 Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count numbers with difference between number and its digit sum greater than specific value
- Queries for the difference between the count of composite and prime numbers in a given range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Equal Sum and XOR of three Numbers
- Count non-negative triplets with sum equal to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.