Count numbers whose difference with N is equal to XOR with N
Last Updated :
25 Aug, 2022
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 respectively 0, 2, 4, 6.
Approach: The XOR value of two bits will be 1 if both bits have opposite sign, and 0 when both bits are same. So on the basis of the property of XOR, we can say that n \oplus x is always greater than or equal to n-x. The only condition when its value is equal with n-x is bits of x form a subset of bits of n. Because if in the i'th position both x and n has set bits then after xor the value will decrease, and the decreased value will be 2^{i} , where i is 0-based position.
So the answer is the total count of subsets of bits of number n is 2^{k} , where k is the count of set bits in n.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// function to Count all values of x
void count_values(int n)
{
// Count set bits in n
// by using stl function
int set_bits = __builtin_popcount(n);
// count all subset of set bits
cout << pow(2, set_bits) << "\n";
}
// Driver code
int main()
{
int n = 27;
count_values(n);
return 0;
}
Java
import java.util.*;
class Solution
{
//count number of set bits
static int __builtin_popcount(int n)
{
//count variable
int count=0;
while(n>0)
{
//if the bit is 1
if(n%2==1)
count++;
n=n/2;
}
return count;
}
// function to Count all values of x
static void count_values(int n)
{
// Count set bits in n
// by using stl function
int set_bits = __builtin_popcount(n);
// count all subset of set bits
System.out.println((int)Math.pow(2, set_bits));
}
// Driver code
public static void main(String args[])
{
int n = 27;
count_values(n);
}
}
// This code is contributed
// by Arnab Kundu
Python 3
# Python3 program to implement
# above approach
# from math import pow method
from math import pow
# count number of set bits
def __builtin_popcount(n) :
# count variable
count = 0
while n > 0 :
# if the bit is 1
if n % 2 == 1 :
count += 1
n = n//2
return count
# function to Count all values of x
def count_values(n) :
set_bits = __builtin_popcount(n)
# count all subset of set bits
print(int(pow(2, set_bits)))
# Driver code
if __name__ == "__main__" :
n = 27
count_values(n)
# This code is contributed by
# ANKITRAI1
C#
using System;
class GFG
{
// count number of set bits
static int __builtin_popcount(int n)
{
// count variable
int count = 0;
while(n > 0)
{
//if the bit is 1
if(n % 2 == 1)
count++;
n = n / 2;
}
return count;
}
// function to Count all values of x
static void count_values(int n)
{
// Count set bits in n
// by using stl function
int set_bits = __builtin_popcount(n);
// count all subset of set bits
Console.Write((int)Math.Pow(2, set_bits));
}
// Driver code
public static void Main()
{
int n = 27;
count_values(n);
}
}
// This code is contributed by Smitha
PHP
<?php
// count number of set bits
function __builtin_popcount($n)
{
// count variable
$count = 0;
while($n > 0)
{
//if the bit is 1
if($n % 2 == 1)
$count++;
$n = $n / 2;
}
return $count;
}
// function to Count all values of x
function count_values($n)
{
// Count set bits in n
// by using stl function
$set_bits = __builtin_popcount($n);
// count all subset of set bits
echo (int)pow(2, $set_bits);
}
// Driver code
$n = 27;
count_values($n);
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>
JavaScript
<script>
// count number of set bits
function __builtin_popcount(n)
{
// count variable
let count = 0;
while(n > 0)
{
//if the bit is 1
if(n % 2 == 1)
count++;
n = parseInt(n / 2);
}
return count;
}
// function to Count all values of x
function count_values(n)
{
// Count set bits in n
// by using stl function
let set_bits = __builtin_popcount(n);
// count all subset of set bits
document.write(Math.pow(2, set_bits) + "<br>");
}
// Driver code
let n = 27;
count_values(n);
</script>
Time Complexity: O(k), where k is number of set bits in N.
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 sum with x is equal to XOR with x Given a integer âxâ, find the number of values of âaâ satisfying the following conditions: 0 <= a <= xa 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 =
9 min read
Count three-digit numbers having difference X with its reverse Given an integer X, the task is to count the total number of three-digit numbers having difference X with its reverse. If no such number exists, then print -1. Examples: Input: X = 792Output : 10Explanation :901 - 109 = 792911 - 119 = 792921 - 129 = 792931 - 139 = 792941 - 149 = 792951 - 159 = 79296
6 min read
Find N distinct numbers whose Bitwise XOR is equal to K Given two positive integers N and K, the task is to construct N positive integers having Bitwise XOR of all these integers equal to K. Examples: Input: N = 4, K = 6Output: 1 0 2 5Explanation: Bitwise XOR the integers {1, 0, 2, 5} = 1 XOR 0 XOR 2 XOR 5 = 6(= K). Input: N = 1, K = 1Output: 1 Approach:
11 min read
Count pairs with absolute difference equal to k Given an array arr[] and a positive integer k, the task is to count all pairs (i, j) such that i < j and absolute value of (arr[i] - arr[j]) is equal to k. Examples: Input: arr[] = [1, 4, 1, 4, 5], k = 3Output: 4Explanation: There are 4 pairs with absolute difference 3, the pairs are [1, 4], [1,
15+ min read