Count smaller values whose XOR with x is greater than x
Last Updated :
12 Jun, 2022
Given a integer ‘x’, find the number of values of ‘a’ satisfying the following conditions:
- a XOR x > x
- 0 < 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 = 13
Input : x = 2
Output : 1
Explanation: For x=2, we have just one value
1 XOR 2 = 3.
Naive Approach
A Simple approach is to check for all values of ‘a’ between 0 and ‘x’ and calculate its XOR with x and check if the condition 1 satisfies.
C++
#include<bits/stdc++.h>
using namespace std;
int countValues( int x)
{
int count = 0;
for ( int i=1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
int main()
{
int x = 10;
cout << countValues(x);
return 0;
}
|
Java
public class XOR
{
static int countValues( int x)
{
int count = 0 ;
for ( int i= 1 ; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
public static void main (String[] args)
{
int x = 10 ;
System.out.println(countValues(x));
}
}
|
Python3
def countValues(x):
count = 0
for i in range ( 1 ,x):
if ((i ^ x) > x):
count + = 1
return count
x = 10
print (countValues(x))
|
C#
using System;
class GFG
{
static int countValues( int x)
{
int count = 0;
for ( int i = 1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
public static void Main ()
{
int x = 10;
Console.Write(countValues(x));
}
}
|
PHP
<?php
function countValues( $x )
{
$count = 0;
for ( $i = 1; $i < $x ; $i ++)
if (( $i ^ $x ) > $x )
$count ++;
return $count ;
}
$x = 10;
echo countValues( $x );
?>
|
Javascript
<script>
function countValues(x)
{
let count = 0;
for (let i=1; i < x; i++)
if ((i ^ x) > x)
count++;
return count;
}
let x = 10;
document.write(countValues(x));
</script>
|
Output :
5
The time complexity of the above approach is O(x).
Auxiliary Space: O(1)
Efficient Approach
The efficient solution lies in the binary representation of the number. We consider all 0’s in binary representation. For every 0 at the i-th position, we can have 2i numbers smaller than or equal to x with greater XOR.
C++
#include<bits/stdc++.h>
using namespace std;
int countValues( int x)
{
int count = 0, n = 1;
while (x != 0)
{
if (x%2 == 0)
count += n;
n *= 2;
x /= 2;
}
return count;
}
int main()
{
int x = 10;
cout << countValues(x);
return 0;
}
|
Java
class GFG
{
static int countValues( int x)
{
int count = 0 , n = 1 ;
while (x != 0 )
{
if (x % 2 == 0 )
count += n;
n *= 2 ;
x /= 2 ;
}
return count;
}
public static void main (String[] args)
{
int x = 10 ;
System.out.println(countValues(x));
}
}
|
Python3
def countValues(x):
count = 0 ;
n = 1 ;
while (x > 0 ):
if (x % 2 = = 0 ):
count + = n;
n * = 2 ;
x / = 2 ;
x = int (x);
return count;
x = 10 ;
print (countValues(x));
|
C#
using System;
class GFG
{
static int countValues( int x)
{
int count = 0, n = 1;
while (x != 0)
{
if (x % 2 == 0)
count += n;
n *= 2;
x /= 2;
}
return count;
}
public static void Main ()
{
int x = 10;
Console.Write(countValues(x));
}
}
|
PHP
<?php
function countValues( $x )
{
$count = 0;
$n = 1;
while ( $x != 0)
{
if ( $x % 2 == 0)
$count += $n ;
$n *= 2;
$x /= 2;
$x = (int) $x ;
}
return $count ;
}
$x = 10;
echo countValues( $x );
?>
|
Javascript
<script>
function countValues(x)
{
var count = 0, n = 1;
while (x != 0)
{
if (x % 2 == 0)
count += n;
n *= 2;
x = parseInt(x / 2);
}
return count;
}
var x = 10;
document.write(countValues(x));
</script>
|
Output :
5
Time complexity: O(Log x).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...