Count of all possible values of X whose Bitwise XOR with N is greater than N
Last Updated :
15 Mar, 2022
Given an integer N count the number of values of X such that X⊕N > N, where ⊕ denotes bitwise XOR operation
Examples:
Input: N = 10
Output: 5
Explanation: The five possible value satisfying the above condition are:
1⊕10 = 11, 4⊕10 = 14, 5⊕10 = 15, 6⊕10 = 12, 7⊕10 = 13
Input: N = 8
Output: 7
Explanation: The seven possible value satisfying the above condition are:
1⊕8 = 9, 2⊕8 = 10, 3⊕8 = 11, 4⊕8 = 12, 5⊕8 = 13, 6⊕8 = 14, 7⊕8 = 15
Naive Approach: The simple approach to this problem is to iterate from 1 to N and increment the count if X XOR N >N for 0 < X < N. Finally, return the count of the number of values.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The most efficient approach is to find the number nearest to the next power of 2 which has all its bits set and finally subtract it from the original number. Given below is the mathematical observation of the solution:
If number N can be written by using k bits then number X must use k-1 bits at most because:-
- If a number X has a same bits as number N Xoring both the number will result in a number less than N which wont satisfy our condition X xor N > N.
- If a number X is greater than number N then Xoring both the number will always result X is greater than N which wont satisfy our inequality.
So the problem reduces to find the count of the number that lies in the given range [x , 2k - 1]. We are taking 2k - 1 because this is the maximum number that can be form when all its n bits are set. - The require number equals 2k - 1 -N .
Follow the steps below to solve the problem:
- Find the nearest next power of 2.
- Subtract 1 from the nearest power of 2 so that the given number has all its bit set
- Finally, subtract from the original number and return it
Below is the implementation of the above approach:
C++
// C++ program for find the count of
// number of value of X such that N XOR X >N
#include <bits/stdc++.h>
using namespace std;
// Function for calculating the total
// possible value satisfy the condition
// X⊕N > N and 0 < X < N
void maximumXor(long long N)
{
long long num = N;
long long int count;
count = 0;
while (N > 0) {
// Count the total number of bits
count++;
N = N / 2;
}
long long next_power = ((long long)1 << count);
// Finding the next power of 2
// of the given number
long long result = next_power - 1;
// Finding the number nearest to
// the nextpower of 2 which has all
// its bits set
cout << result - num;
}
// Driver Code
int main()
{
int N = 10;
maximumXor(N);
return 0;
}
Java
// Java program for find the count of
// number of value of X such that N XOR X >N
import java.util.*;
public class GFG {
// Function for calculating the total
// possible value satisfy the condition
// X⊕N > N and 0 < X < N
static void maximumXor(long N)
{
long num = N;
long count = 0;
count = 0;
while (N > 0) {
// Count the total number of bits
count++;
N = N / 2;
}
long next_power = ((long)1 << count);
// Finding the next power of 2
// of the given number
long result = next_power - 1;
// Finding the number nearest to
// the nextpower of 2 which has all
// its bits set
System.out.print(result - num);
}
// Driver Code
public static void main(String args[])
{
int N = 10;
maximumXor(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 program for find the count of
# number of value of X such that N XOR X >N
# Function for calculating the total
# possible value satisfy the condition
# X⊕N > N and 0 < X < N
def maximumXor(N):
num = N
count = 0
while (N > 0):
# Count the total number of bits
count += 1
N = N // 2
next_power = (1 << count)
# Finding the next power of 2
# of the given number
result = next_power - 1
# Finding the number nearest to
# the nextpower of 2 which has all
# its bits set
print(result - num)
# Driver Code
if __name__ == "__main__":
N = 10
maximumXor(N)
# This code is contributed by rakeshsahni
C#
// C# program for find the count of
// number of value of X such that N XOR X >N
using System;
public class GFG
{
// Function for calculating the total
// possible value satisfy the condition
// X⊕N > N and 0 < X < N
static void maximumXor(long N)
{
long num = N;
long count = 0;
count = 0;
while (N > 0) {
// Count the total number of bits
count++;
N = N / 2;
}
long next_power = (1 << (int)count);
// Finding the next power of 2
// of the given number
long result = next_power - 1;
// Finding the number nearest to
// the nextpower of 2 which has all
// its bits set
Console.Write(result - num);
}
// Driver Code
public static void Main(String []args)
{
int N = 10;
maximumXor(N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript code for the above approach
// Function for calculating the total
// possible value satisfy the condition
// X⊕N > N and 0 < X < N
function maximumXor(N) {
let num = N;
let count;
count = 0;
while (N > 0) {
// Count the total number of bits
count++;
N = Math.floor(N / 2);
}
let next_power = (1 << count);
// Finding the next power of 2
// of the given number
let result = next_power - 1;
// Finding the number nearest to
// the nextpower of 2 which has all
// its bits set
document.write(result - num);
}
// Driver Code
let N = 10;
maximumXor(N);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Similar Reads
Count smaller values whose XOR with x is greater than x Given a integer 'x', find the number of values of 'a' satisfying the following conditions: a XOR x > x0 < a < x Examples : Input : x = 10 Output : 5 Explanation: For x = 10, following 5 values of 'a' satisfy the conditions: 1 XOR 10 = 11 4 XOR 10 = 14 5 XOR 10 = 15 6 XOR 10 = 12 7 XOR 10 =
7 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15
4 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2 Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value Examples: Input : arr[]={ 12, 4 , 15}Output: 2Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4
6 min read
Count of possible pairs whose sum and bitwise XOR is given Given two integers S and X representing the sum and bitwise XOR respectively of two integers, the task is to find the count of all such possible pairs such that their sum is equal to S and bitwise XOR is equal to X. Examples: Input: S = 9, X = 5Output: 4Explanation: (2, 7), (3, 6), (6, 3), (7, 2) co
9 min read
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read
Count distinct possible Bitwise XOR values of subsets of an array Given an array arr[] consisting of N integers, the task is to find the size of the set S such that Bitwise XOR of any subset of the array arr[] exists in the set S. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 8Explanation:All possible Bitwise XOR values of subsets of the array arr[] are {0, 1, 2
14 min read