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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Comparing leading zeros in binary representations of two numbers
- XOR of two numbers after making length of their binary representations equal
- 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
- Check whether two Strings are Anagram of each other using HashMap in Java
- Python sorted() to check if two strings are anagram or not
- Check divisibility of binary string by 2^k
- Check if binary string multiple of 3 using DFA
- Check divisibility in a binary stream
- 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)
- Longest Common Anagram Subsequence
- Check if binary representation of a number is palindrome
- Check if all the set bits of the binary representation of N are at least K places away
- Check whether the binary equivalent of a number ends with "001" or not
- Fibbinary Numbers (No consecutive 1s in binary) - O(1) Approach
- Count of N-bit binary numbers without leading zeros
- Convert numbers into binary representation and add them without carry