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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if binary representations of 0 to N are present as substrings in given binary string
- 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
- Python sorted() to check if two strings are anagram or not
- Check whether two Strings are Anagram of each other using HashMap in Java
- Remove minimum number of characters so that two strings become anagram
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Check if any anagram of a string is palindrome or not
- Anagram Substring Search (Or Search for all permutations)
- Anagram checking in Python using collections.Counter()
- Find the size of largest subset of anagram words
- Longest Common Anagram Subsequence
- Python Counter to find the size of largest subset of anagram words
- Convert string X to an anagram of string Y with minimum replacements
- Longest common anagram subsequence from N strings
- Number of sub-strings which are anagram of any sub-string of another string
- Minimum number of adjacent swaps to convert a string into its given anagram