Count numbers whose sum with x is equal to XOR with x
Last Updated :
10 Jun, 2022
Given a integer ‘x’, find the number of values of ‘a’ satisfying the following conditions:
- 0 <= a <= x
- a XOR x = a + x
Examples :
Input : 5
Output : 2
Explanation:
For x = 5, following 2 values
of 'a' satisfy the conditions:
5 XOR 0 = 5+0
5 XOR 2 = 5+2
Input : 10
Output : 4
Explanation:
For x = 10, following 4 values
of 'a' satisfy the conditions:
10 XOR 0 = 10+0
10 XOR 1 = 10+1
10 XOR 4 = 10+4
10 XOR 5 = 10+5
Naive Approach:
A Simple approach is to check for all values of ‘a’ between 0 and ‘x’ (both inclusive) and calculate its XOR with x and check if the condition 2 satisfies.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int FindValues( int x)
{
int count = 0;
for ( int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
int main()
{
int x = 10;
cout << FindValues(x);
return 0;
}
|
Java
class Fib
{
static int FindValues( int x)
{
int count = 0 ;
for ( int i = 0 ; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
public static void main(String[] args)
{
int x = 10 ;
System.out.println(FindValues(x));
}
}
|
Python3
def FindValues(x):
count = 0
for i in range ( 0 , x):
if ((x + i) = = (x ^ i)):
count = count + 1
return count
x = 10
print (FindValues(x))
|
C#
using System;
class Fib
{
static int FindValues( int x)
{
int count = 0;
for ( int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
public static void Main()
{
int x = 10;
Console.Write(FindValues(x));
}
}
|
PHP
<?php
function FindValues( $x )
{
$count = 0;
for ( $i = 0; $i <= $x ; $i ++)
if (( $x + $i ) == ( $x ^ $i ))
$count ++;
return $count ;
}
$x = 10;
echo FindValues( $x );
?>
|
Javascript
<script>
function FindValues(x)
{
let count = 0;
for (let i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
let x = 10;
document.write(FindValues(x));
</script>
|
Time complexity: O(x).
Auxiliary Space: O(1)
Efficient Approach:
XOR simulates binary addition without the carry over to the next digit. For the zero digits of ‘a’ we can either add a 1 or 0 without getting a carry which implies xor = + whereas if a digit in ‘a’ is 1 then the matching digit in x is forced to be 0 in order to avoid carry. For each 0 in ‘a’ in the matching digit in x can either being a 1 or 0 with a total combination count of 2^(num of zero). Hence, we just need to count the number of 0’s in binary representation of the number and answer will by 2^(number of zeroes).
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
long CountZeroBit( long x)
{
unsigned int count = 0;
while (x)
{
if (!(x & 1LL))
count++;
x >>= 1LL;
}
return count;
}
long CountXORandSumEqual( long x)
{
long count = CountZeroBit(x);
return (1LL << count);
}
int main()
{
long x = 10;
cout << CountXORandSumEqual(x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long CountZeroBit( long x)
{
long count = 0 ;
while (x > 0 ) {
if ((x & 1L) == 0 )
count++;
x >>= 1L;
}
return count;
}
static long CountXORandSumEqual( long x)
{
long count = CountZeroBit(x);
return (1L << count);
}
public static void main(String[] args)
{
long x = 10 ;
System.out.println(CountXORandSumEqual(x));
}
}
|
Python3
def CountZeroBit(x):
count = 0
while (x):
if ((x & 1 ) = = 0 ):
count + = 1
x >> = 1
return count
def CountXORandSumEqual(x):
count = CountZeroBit(x)
return ( 1 << count)
if __name__ = = '__main__' :
x = 10
print (CountXORandSumEqual(x))
|
C#
using System;
class GFG {
static int CountZeroBit( int x)
{
int count = 0;
while (x > 0) {
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
static int CountXORandSumEqual( int x)
{
int count = CountZeroBit(x);
return (1 << count);
}
static public void Main()
{
int x = 10;
Console.WriteLine(CountXORandSumEqual(x));
}
}
|
PHP
<?php
function CountZeroBit( $x )
{
$count = 0;
while ( $x )
{
if (!( $x & 1))
$count ++;
$x >>= 1;
}
return $count ;
}
function CountXORandSumEqual( $x )
{
$count = CountZeroBit( $x );
return (1 << $count );
}
$x = 10;
echo CountXORandSumEqual( $x );
?>
|
Javascript
<script>
function CountZeroBit(x)
{
let count = 0;
while (x > 0)
{
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
function CountXORandSumEqual(x)
{
let count = CountZeroBit(x);
return (1 << count);
}
let x = 10;
document.write(CountXORandSumEqual(x));
</script>
|
Time complexity: O(Log x)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...