Given two numbers you are required to check whether they are anagrams of each other or not in binary representation.
Input : a = 8, b = 4 Output : Yes Binary representations of both numbers have same 0s and 1s. Input : a = 4, b = 5 Output : No
- Find Binary Representation of ‘a’ and ‘b’ using simple decimal to binary representation technique.
- Check if two binary representations are anagram
Time Complexity : O (n log n)
Auxiliary Space : O (1) Although Auxiliary Space is O(1) still SIZE array spaces are getting used to store binary representation of each number.
Just measure the number of 1’s present in the bit representation of both the numbers, if number of 1’s present in their bit representation are same then they are anagrams in their bit representation else they are not.
Note that 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.
Time Complexity : O (1)
Auxiliary Space : O (1) No extra space is getting used.
This article is contributed by Aditya Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- XOR of two numbers after making length of their binary representations equal
- Comparing leading zeros in binary representations of two numbers
- Check if binary representation of a given number and its complement are anagram
- Check whether two strings are anagram of each other
- Check if any anagram of a string is palindrome or not
- Python sorted() to check if two strings are anagram or not
- Check whether two Strings are Anagram of each other using HashMap in Java
- Check divisibility of binary string by 2^k
- Check divisibility in a binary stream
- Check if binary string multiple of 3 using DFA
- 1 to n bit numbers with no consecutive 1s in binary representation
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Fibbinary Numbers (No consecutive 1s in binary)
- Check if binary representation of a number is palindrome
- Fibbinary Numbers (No consecutive 1s in binary) - O(1) Approach