VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-binary-representation-given-number-complement-anagram/

⇱ Check if binary representation of a given number and its complement are anagram - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 30 Mar, 2023

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


 


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.
 

Output: 

1

Time Complexity : O(logn*log(logn)) where n is the given number.

Auxiliary Space: O(ULL_SIZE) where ULL_SIZE is defined constant.
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. 
 

Output: 

1

Time Complexity : O(1)

Auxiliary Space: O(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.
 

Comment