Count of x in given range such that bitwise XOR of x, (x+1) and (x+2), (x+3) are equal
Last Updated :
20 Apr, 2023
Given an integer N, the task is to count the number of integers (say x) in the range [0, 2N−1] such that x⊕(x+1) = (x+2)⊕(x+3). [where ⊕ represents bitwise Xor]
Examples:
Input: N = 1
Output: 1
Explanation: Only 0 is the valid x, as, 0 ⊕ 1 = 2 ⊕ 3 = 1
Input: N = 3
Output: 4
Naive Approach: The simple approach to solve the given problem is to generate all possible values of x in the range [0, 2N−1] and check if they satisfy the given criteria i.e x⊕(x+1) = (x+2)⊕(x+3).
Follow the steps mentioned below to implement the idea:
- Iterate from i = 0 to N.
- Check if (i ⊕ (i+1)) = ((i+2) ⊕ (i+3))
- If the condition is satisfied increment the count of such numbers.
- Return the final count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOfvalues( int N)
{
int count = 0;
for ( int i = 0; i < (1 << N); i++) {
if ((i ^ (i + 1)) == ((i + 2) ^ (i + 3)))
count += 1;
}
return count;
}
int main()
{
int N = 3;
cout << countOfvalues(N);
return 0;
}
|
Java
class GFG {
public static int countOfvalues( int N)
{
int count = 0 ;
for ( int i = 0 ; i < ( 1 << N); i++) {
if ((i ^ (i + 1 )) == ((i + 2 ) ^ (i + 3 )))
count += 1 ;
}
return count;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(countOfvalues(N));
}
}
|
Python3
def countOfvalues(N):
count = 0
for i in range ( 0 , 2 * * N):
if i ^ (i + 1 ) = = (i + 2 ) ^ (i + 3 ):
count + = 1
return count
if __name__ = = '__main__' :
N = 3
print (countOfvalues(N))
|
C#
using System;
class GFG
{
public static int countOfvalues( int N)
{
int count = 0;
for ( int i = 0; i < (1 << N); i++) {
if ((i ^ (i + 1)) == ((i + 2) ^ (i + 3)))
count += 1;
}
return count;
}
public static void Main ()
{
int N = 3;
Console.Write(countOfvalues(N));
}
}
|
Javascript
<script>
function countOfvalues(N)
{
let count = 0;
for (let i = 0; i < (1 << N); i++) {
if ((i ^ (i + 1)) == ((i + 2) ^ (i + 3)))
count += 1;
}
return count;
}
let N = 3;
document.write(countOfvalues(N));
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved efficiently based on the following mathematical idea:
- If x is such that it is a even number then x+2 is also a even number and both (x+1) and (x+3) will be odd numbers.
- Now two consecutive even and odd number has only a single bit difference only on their LSB position.
- So the bitwise XOR of (x and x+1) and (x+2 and x+3) both will be 1, when x is even.
- Therefore all the even numbers in the given range [0, 2N−1] is a possible value of x.
So total number of such values are (2N – 1)/2 = 2N-1
Follow the illustration below to visualize the idea better:
Illustration:
Consider N = 3. So the numbers are in range [0, 7]
All even numbers in the range are 0, 2, 4 and 6
=> When x = 0: 0 ⊕ 1 = 1 and 2 ⊕ 3 = 1. Therefore the relation holds
=> When x = 2: 2 ⊕ 3 = 1 and 4 ⊕ 5 = 1. Therefore the relation holds
=> When x = 4. 4 ⊕ 5 = 1 and 6 ⊕ 7 = 1. Therefore the relation holds
=> When x = 6: 6 ⊕ 7 = 1 and 8 ⊕ 9 = 1. Therefore the relation holds.
Now for the odd numbers if it is done:
=> When x = 1: 1 ⊕ 2 = 3 and 3 ⊕ 4 = 7. Therefore the relation does not hold.
=> When x = 3: 3 ⊕ 4 = 7 and 5 ⊕ 6 = 3. Therefore the relation does not hold.
=> When x = 5: 5 ⊕ 6 = 3 and 7 ⊕ 8 = 15. Therefore the relation does not hold.
=> When x = 7: 7 ⊕ 8 = 15 and 9 ⊕ 10 = 3. Therefore the relation does not hold.
So total possible values are 4 = 23-1
Therefore to get the answer calculate the value of 2N-1 for given N
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countValues( int N) { return pow (2, N - 1); }
int main()
{
int N = 3;
cout << countValues(N);
return 0;
}
|
Java
class GFG {
public static int countValues( int N) { return ( int )Math.pow( 2 , N - 1 ); }
public static void main(String[] args) {
int N = 3 ;
System.out.println(countValues(N));
}
}
|
Python3
def countOfValues (N):
return pow ( 2 , N - 1 )
if __name__ = = '__main__' :
N = 3
res = countOfValues(N)
print (res)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int countValues( int N)
{
return ( int )Math.Pow(2, N - 1);
}
public static void Main(String[] args)
{
int N = 3;
Console.Write(countValues(N));
}
}
|
Javascript
<script>
function countValues(N)
{
return Math.pow(2, N - 1);
}
let N = 3;
document.write(countValues(N));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...