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.
CPP
#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;
bool binary_a[ULL_SIZE] = { 0 };
for ( int i = 0; a > 0; i++) {
binary_a[i] = a % 2;
a /= 2;
}
bool binary_b[ULL_SIZE] = { 0 };
for ( int i = 0; b > 0; i++) {
binary_b[i] = b % 2;
b /= 2;
}
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 ;
}
int main()
{
ull a = 4294967295;
cout << isComplementAnagram(a) << endl;
return 0;
}
|
Java
import java.math.BigInteger;
import java.util.*;
class GFG {
static byte [] toUnsigned( byte [] byteArray)
{
BigInteger b = new BigInteger(byteArray);
b = b.ONE.shiftLeft( 128 );
return b.toByteArray();
}
static int isComplementAnagram(BigInteger a)
{
byte [] binary_a = a.toByteArray();
binary_a = toUnsigned(binary_a);
byte [] binary_b = new byte [binary_a.length];
for ( int i = 0 ; i < binary_a.length; i++) {
binary_b[i] = ( byte )(binary_a[i] ^ 0xFF );
}
binary_b = toUnsigned(binary_b);
Arrays.sort(binary_a);
Arrays.sort(binary_b);
binary_a = toUnsigned(binary_b);
binary_b = toUnsigned(binary_b);
for ( int i = 0 ; i < binary_a.length; i++)
if (binary_a[i] != binary_b[i])
return 0 ;
return 1 ;
}
public static void main(String[] args)
{
BigInteger a = new BigInteger( "4294967295" );
System.out.println(isComplementAnagram(a));
}
}
|
Python3
ULL_SIZE = 8 * 8
def isComplementAnagram(a):
binary_a = [ 0 for _ in range (ULL_SIZE)]
binary_b = [ 0 for _ in range (ULL_SIZE)]
i = 0
while a > 0 :
binary_a[i] = a % 2
a = int (a / 2 )
i + = 1
for i in range (ULL_SIZE):
binary_b[i] = 1 - binary_a[i]
binary_a.sort()
binary_b.sort()
for i in range (ULL_SIZE):
if (binary_a[i] ! = binary_b[i]):
return 0
return 1
a = 4294967295
print (isComplementAnagram(a))
|
Javascript
const ULL_SIZE = 8 * 8;
function isComplementAnagram(a)
{
var binary_a = new Array(ULL_SIZE).fill(0);
var binary_b = new Array(ULL_SIZE).fill(0);
for ( var i = 0; a > 0; i++)
{
binary_a[i] = a % 2;
a = Math.floor(a / 2);
}
for ( var i = 0; i < ULL_SIZE; i++)
{
binary_b[i] = 1 - binary_a[i];
}
binary_a.sort();
binary_b.sort();
for ( var i = 0; i < ULL_SIZE; i++)
if (binary_a[i] != binary_b[i])
return false ;
return true ;
}
var a = 4294967295;
console.log(isComplementAnagram(a));
|
C#
using System;
using System.Linq;
public class GFG {
const int ULL_SIZE = 8 * 8;
static bool IsComplementAnagram( ulong a)
{
var binary_a = new int [ULL_SIZE];
var binary_b = new int [ULL_SIZE];
for ( int i = 0; a > 0; i++) {
binary_a[i] = ( int )(a % 2);
a = ( ulong )Math.Floor(( double )a / 2);
}
for ( int i = 0; i < ULL_SIZE; i++) {
binary_b[i] = 1 - binary_a[i];
}
Array.Sort(binary_a);
Array.Sort(binary_b);
for ( int i = 0; i < ULL_SIZE; i++) {
if (binary_a[i] != binary_b[i]) {
return false ;
}
}
return true ;
}
static public void Main()
{
ulong a = 4294967295;
Console.WriteLine(IsComplementAnagram(a) ? 1 : 0);
}
}
|
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.
C++
#include <bits/stdc++.h>
#define ull unsigned long long int
using namespace std;
const int ULL_SIZE = 8* sizeof (ull);
bool bit_anagram_check(ull a)
{
return (_popcnt64(a) == (ULL_SIZE >> 1));
}
int main()
{
ull a = 4294967295;
cout << bit_anagram_check(a) << endl;
return 0;
}
|
Java
class GFG
{
static byte longSize = 8 ;
static int ULL_SIZE = 8 *longSize;
static boolean bit_anagram_check( long a)
{
return (Integer.bitCount(( int )a) == (ULL_SIZE >> 1 ));
}
public static void main(String[] args)
{
long a = 4294967295L;
System.out.println(bit_anagram_check(a));
}
}
|
Python3
ULL_SIZE = 64
def bit_anagram_check(a):
return ( bin (a).count( "1" ) = = (ULL_SIZE >> 1 ))
a = 4294967295
print ( int (bit_anagram_check(a)))
|
C#
using System;
class GFG
{
static byte longSize = 8;
static int ULL_SIZE = 8*longSize;
static bool bit_anagram_check( long a)
{
return (BitCount(( int )a) == (ULL_SIZE >> 1));
}
static int BitCount( int n)
{
int count = 0;
while (n != 0)
{
count++;
n &= (n - 1);
}
return count;
}
public static void Main(String[] args)
{
long a = 4294967295L;
Console.WriteLine(bit_anagram_check(a));
}
}
|
PHP
<?php
function bit_anagram_check( $a )
{
$longSize = 8;
$ULL_SIZE = 8 * $longSize ;
return (BitCount( $a ) == ( $ULL_SIZE >> 1));
}
function BitCount( $n )
{
$count = 0;
while ( $n != 0)
{
$count ++;
$n &= ( $n - 1);
}
return $count ;
}
$a = 4294967295;
echo (bit_anagram_check( $a ));
?>
|
Javascript
<script>
var longSize = 8;
var ULL_SIZE = 8*longSize;
function bit_anagram_check(a)
{
var ans =a.toString(2).split( '0' ).join( '' ).length
== (ULL_SIZE >> 1)?1:0;
return ans;
}
var a = 4294967295;
document.write(bit_anagram_check(a));
</script>
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Mar, 2023
Like Article
Save Article