Count of values of x <= n for which (n XOR x) = (n – x) in C++

C++Server Side ProgrammingProgramming

We are given a number n as input. The goal is to find values x such that condition (n xor x)=(nx) holds. Also x lies in range [0,n].

Let us understand with examples

Input − n=10

Output − Count of values of x <= n for which (n XOR x) = (n – x) are − 4

Explanation − Values of x with 10 xor x = 10-x − 0, 2, 8 and 10.

Input − n=15

Output − Count of values of x <= n for which (n XOR x) = (n – x) are − 16

Explanation − Values of x with 15 xor x = 15-x − 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 and 15.

Approach used in the below program is as follows

We will use two approaches. First naive approach using for loops. Start traversing from i=0 to i<=n for i as possible x’s. Now check if ( n - i == (n ^ i) //xor ). If true increment count.

  • Take integer variable n as input.

  • Function unique_pair(int arr[], int size) takes array and its length and returns the number of unique pairs such that in pair (arr[i],arr[j]) index i<j

  • Take the initial value of count as 0.

  • Take a set ‘se’ containing integer pairs. (set<pair<int, int>> se)

  • Start traversing arr[] using two for loops. from i=0 to i<size-1 and j=i+1 to j<size.

  • For each pair always i<j, add pair (arr[i],arr[j]) to ‘se’ using se.insert(make_pair(arr[i], arr[j]));

  • At the end of both for loop, update count=se.size().

  • Count now has a number of pairs in ‘se’. ( All are unique ).

  • Return count as result.

Efficient Approach

In this approach we will first convert n to its binary equivalent. We know that

1 xor 0 = 1-0

1 xor 1 = 1-1

But

0 xor 0 ≠ 0-1

0 xor 1 ≠ 0-1

So for every 1 in binary representation of n, there are 2 cases. For p ones in binary representation of n, there will be 2p values that satisfy the condition.

index i. Then add individual such counts for total unique pairs.

  • Take integer variable n as input.

  • Function unique_pair(int arr[], int size) takes array and its length and returns the number of unique pairs such that in pair (arr[i],arr[j]) index i<j

  • Take the initial value of count as 0.

  • Convert n to string using number=bitset<8>(n).to_string();

  • Take length=number.length().

  • Traverse string using for loop from index i=0 to i<length. For each 1 increment count.

  • Set count=pow(2,count) as final values of x.

  • Return count as result.

Example (naive approach)

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int count_values(int n){
   int count = 0;
   for (int i = 0; i <= n; i++){
      if (n - i == (n ^ i)){
         count++;
      }
   }
   return count;
}
int main(){
   int n = 25;
   cout<<"Count of values of x <= n for which (n XOR x) = (n – x) are: "<<count_values(n);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of values of x <= n for which (n XOR x) = (n – x) are: 8

Example (Efficient Approach)

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int count_values(int n){
   int count = 0;
   string number = bitset<8>(n).to_string();
   int length = number.length();
   for (int i = 0; i < length; i++){
      if (number.at(i) == '1')
         { count++; }
   }
   count = (int)pow(2, count);
   return count;
}
int main(){
   int n = 25;
   cout<<"Count of values of x <= n for which (n XOR x) = (n – x) are: "<<count_values(n);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of values of x <= n for which (n XOR x) = (n – x) are: 8
raja
Published on 02-Dec-2020 12:08:16
Advertisements