# Check if binary representation of a given number and its complement are anagram

Given a positive number you need to check whether it’s complement and the number are anagrams or not.

Examples:

```Input : a = 4294967295
Output : Yes
Binary representation of 'a' and it's
complement are anagrams of each other

Input : a = 4
Output : No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach: In this approach calculation of the complement of the number is allowed.

1. Find binary representation of the number and it’s complement using simple decimal to binary representation technique.

2. Sort both the binary representations and compare them to check whether they are anagrams or not.

```// A simple C++ program to check if binary
// representations of a number and it's
// complement are anagram.
#include <bits/stdc++.h>
#define ull unsigned long long int
using namespace std;

const int ULL_SIZE = 8*sizeof(ull);

bool isComplementAnagram(ull a)
{
ull b = ~a; // Finding complement of a;

// Find reverse binary representation of a.
bool binary_a[ULL_SIZE] = { 0 };
for (int i=0; a > 0; i++)
{
binary_a[i] = a % 2;
a /= 2;
}

// Find reverse binary representation
// of complement.
bool binary_b[ULL_SIZE] = { 0 };
for (int i=0; b > 0; i++)
{
binary_b[i] = b % 2;
b /= 2;
}

// Sort binary representations and compare
// after sorting.
sort(binary_a, binary_a + ULL_SIZE);
sort(binary_b, binary_b + ULL_SIZE);
for (int i = 0; i < ULL_SIZE; i++)
if (binary_a[i] != binary_b[i])
return false;

return true;
}

// Driver code
int main()
{
ull a = 4294967295;
cout << isComplementAnagram(a) << endl;
return 0;
}
```

Output:

```1
```

Efficient Approach: Just count the number of 1’s present in the bit representation of the given number. If number of 1’s present are 32 then it’s complement will also have 32 1’s in it’s bit representation and they will be anagrams of each other.

```// An efficient C++ program to check if binary
// representations of a number and it's complement are anagram.
#include <bits/stdc++.h>
#define ull unsigned long long int
using namespace std;

const int ULL_SIZE = 8*sizeof(ull);

// Returns true if binary representations of
// a and b are anagram.
bool bit_anagram_check(ull a)
{
// _popcnt64(a) gives number of 1's present
// in binary representation of a. If number
// of 1s is half of total bits, return true.
return (_popcnt64(a) == (ULL_SIZE >> 1));
}

int main()
{
ull a = 4294967295;
cout << bit_anagram_check(a) << endl;
return 0;
}
```

Output:

```1
```

Note:
1. The answer is only dependent on the number, in the above approach we don’t even find the need to obtain the complement of the number.

2. The above code uses GCC specific functions. If we wish to write code for other compilers, we may use Count set bits in an integer.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.5 Average Difficulty : 3.5/5.0
Based on 2 vote(s)