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++
// C++ program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
#include <bits/stdc++.h>
using namespace std;
int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
int main()
{
int x = 10;
// Function call
cout << FindValues(x);
return 0;
}
Java
// Java program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
class Fib
{
static int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
public static void main(String[] args)
{
int x = 10;
// Function call
System.out.println(FindValues(x));
}
}
Python3
# Python3 program to find count of
# values whose XOR with x is equal
# to the sum of value and x and
# values are smaller than equal to x
def FindValues(x):
# Initialize result
count = 0
# Traversing through all values
# between 0 and x both inclusive
# and counting numbers that
# satisfy given property
for i in range(0, x):
if ((x + i) == (x ^ i)):
count = count + 1
return count
# Driver code
x = 10
# Function call
print(FindValues(x))
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
using System;
class Fib
{
static int FindValues(int x)
{
// Initialize result
int count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (int i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
// Driver code
public static void Main()
{
int x = 10;
// Function call
Console.Write(FindValues(x));
}
}
// This code is contributed by Nitin Mittal.
PHP
<?php
// PHP program to find count
// of values whose XOR with x
// is equal to the sum of value
// and x and values are smaller
// than equal to x
// function return the
// value of count
function FindValues($x)
{
// Initialize result
$count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for ($i = 0; $i <= $x; $i++)
if (($x + $i) == ($x ^ $i))
$count++;
return $count;
}
// Driver code
$x = 10;
// Function call
echo FindValues($x);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to find count of values whose XOR
// with x is equal to the sum of value and x
// and values are smaller than equal to x
function FindValues(x)
{
// Initialize result
let count = 0;
// Traversing through all values between
// 0 and x both inclusive and counting
// numbers that satisfy given property
for (let i = 0; i <= x; i++)
if ((x + i) == (x ^ i))
count++;
return count;
}
let x = 10;
// Function call
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++
// C++ program to count numbers whose bitwise
// XOR and sum with x are equal
#include <bits/stdc++.h>
using namespace std;
// Function to find total 0 bit in a number
long CountZeroBit(long x)
{
unsigned int count = 0;
while (x)
{
if (!(x & 1LL))
count++;
x >>= 1LL;
}
return count;
}
// Function to find Count of non-negative numbers
// less than or equal to x, whose bitwise XOR and
// SUM with x are equal.
long CountXORandSumEqual(long x)
{
// count number of zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1LL << count);
}
// Driver code
int main()
{
long x = 10;
// Function call
cout << CountXORandSumEqual(x);
return 0;
}
Java
// Java program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
import java.io.*;
class GFG {
// Function to find total
// 0 bit in a number
static long CountZeroBit(long x)
{
long count = 0;
while (x > 0) {
if ((x & 1L) == 0)
count++;
x >>= 1L;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static long CountXORandSumEqual(long x)
{
// count number of
// zero bit in x
long count = CountZeroBit(x);
// power of 2 to count
return (1L << count);
}
// Driver code
public static void main(String[] args)
{
long x = 10;
// Function call
System.out.println(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
Python3
# Python3 program to count numbers whose bitwise
# XOR and sum with x are equal
# Function to find total 0 bit in a number
def CountZeroBit(x):
count = 0
while (x):
if ((x & 1) == 0):
count += 1
x >>= 1
return count
# Function to find Count of non-negative numbers
# less than or equal to x, whose bitwise XOR and
# SUM with x are equal.
def CountXORandSumEqual(x):
# count number of zero bit in x
count = CountZeroBit(x)
# power of 2 to count
return (1 << count)
# Driver code
if __name__ == '__main__':
x = 10
# Function call
print(CountXORandSumEqual(x))
# This code is contributed by 29AjayKumar
C#
// C# program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
using System;
class GFG {
// Function to find total
// 0 bit in a number
static int CountZeroBit(int x)
{
int count = 0;
while (x > 0) {
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
static int CountXORandSumEqual(int x)
{
// count number of
// zero bit in x
int count = CountZeroBit(x);
// power of 2 to count
return (1 << count);
}
// Driver code
static public void Main()
{
int x = 10;
// Function call
Console.WriteLine(CountXORandSumEqual(x));
}
}
// The code is contributed by ajit
PHP
<?php
// PHP program to count numbers whose bitwise
// XOR and sum with x are equal
// Function to find total 0 bit in a number
function CountZeroBit($x)
{
$count = 0;
while ($x)
{
if (!($x & 1))
$count++;
$x >>= 1;
}
return $count;
}
// Function to find Count of
// non-negative numbers less
// than or equal to x, whose
// bitwise XOR and SUM with
// x are equal.
function CountXORandSumEqual($x)
{
// count number of zero bit in x
$count = CountZeroBit($x);
// power of 2 to count
return (1 << $count);
}
// Driver code
$x = 10;
// Function call
echo CountXORandSumEqual($x);
// This code is contributed by m_kit
?>
JavaScript
<script>
// Javascript program to count
// numbers whose bitwise
// XOR and sum with x
// are equal
// Function to find total
// 0 bit in a number
function CountZeroBit(x)
{
let count = 0;
while (x > 0)
{
if ((x & 1) == 0)
count++;
x >>= 1;
}
return count;
}
// Function to find Count
// of non-negative numbers
// less than or equal to x,
// whose bitwise XOR and
// SUM with x are equal.
function CountXORandSumEqual(x)
{
// count number of
// zero bit in x
let count = CountZeroBit(x);
// power of 2 to count
return (1 << count);
}
// Driver code
let x = 10;
// Function call
document.write(CountXORandSumEqual(x));
// This code is contributed by suresh07
</script>
Time complexity: O(Log x)
Auxiliary Space: O(1)
Similar Reads
Count numbers whose XOR with N is equal to OR with N Given a number N, the task is to find the count of X such that N XOR X == N OR X, where 0<=X<=N Examples: Input: N = 5 Output: 2 For N = 5, 5 XOR 2 == 5 OR 2 5 XOR 0 == 5 OR 0 Thus, count is 2.Input: N = 7 Output: 1 For N = 7, 7 XOR 0 == 7 OR 0 Thus, count is 1. Recommended: Please solve it on
4 min read
Count numbers whose difference with N is equal to XOR with N Given a number N. The task is to count all possible values of x such that n\oplus x is equal to (N-x), where \oplus denotes bitwise XOR operation.Examples: 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 respect
4 min read
Count subarrays with sum equal to its XOR value Given an array arr[] containing N elements, the task is to count the number of sub-arrays whose XOR of all the elements is equal to the sum of all the elements in the subarray. Examples: Input: arr[] = {2, 5, 4, 6} Output: 5 Explanation: All the subarrays {{2}, {5}, {4}, {6}} satisfies the above con
10 min read
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 pairs up to N having sum equal to their XOR Given an integer N, the task is to count the number of pairs (X, Y) such that X + Y = X ^ Y and X + Y ? N. Note: ^ denotes Bitwise xor. Examples: Input: N = 3Output: 9Explanation: The pairs satisfying the given conditions are {{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}, {3, 0}, {0, 3}, {2, 1}, {1, 2}} I
11 min read