Related Articles
Check if B can be formed by permuting the binary digits of A
• Last Updated : 24 Mar, 2021

Given two integer A and B, the task is to check whether the binary representation of B can be generated by permuting the binary digits of A.
Examples:

Input: A = 3, B = 9
Output: Yes
Binary(3) = 0011 and Binary(9) = 1001
Input: A = 6, B = 7
Output: No

Approach: The idea is to count the number of set bits in the binary representations of both the numbers, now if they are equal then the answer is Yes or else the answer is No.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that returns true if the``// binary representation of b can be``// generated by permuting the``// binary digits of a``bool` `isPossible(``int` `a, ``int` `b)``{` `    ``// Find the count of set bits``    ``// in both the integers``    ``int` `cntA = __builtin_popcount(a);``    ``int` `cntB = __builtin_popcount(b);` `    ``// If both the integers have``    ``// equal count of set bits``    ``if` `(cntA == cntB)``        ``return` `true``;``    ``return` `false``;``}` `// Driver code``int` `main()``{``    ``int` `a = 3, b = 9;` `    ``if` `(isPossible(a, b))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `    ``// recursive function to count set bits``    ``public` `static` `int` `countSetBits(``int` `n)``    ``{` `        ``// base case``        ``if` `(n == ``0``)``            ``return` `0``;``        ``else` `            ``// if last bit set add 1 else add 0``            ``return` `(n & ``1``) + countSetBits(n >> ``1``);``    ``}``    ` `    ``// Function that returns true if the``    ``// binary representation of b can be``    ``// generated by permuting the``    ``// binary digits of a``    ``static` `boolean` `isPossible(``int` `a, ``int` `b)``    ``{``    ` `        ``// Find the count of set bits``        ``// in both the integers``        ``int` `cntA = countSetBits(a);``        ``int` `cntB = countSetBits(b);``    ` `        ``// If both the integers have``        ``// equal count of set bits``        ``if` `(cntA == cntB)``            ``return` `true``;``        ``return` `false``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `a = ``3``, b = ``9``;``    ` `        ``if` `(isPossible(a, b))``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the approach` `# function to count set bits``def` `bitsoncount(x):``    ``return` `bin``(x).count(``'1'``)` `# Function that returns true if the``# binary representation of b can be``# generated by permuting the``# binary digits of a``def` `isPossible(a, b):` `    ``# Find the count of set bits``    ``# in both the integers``    ``cntA ``=` `bitsoncount(a);``    ``cntB ``=` `bitsoncount(b);` `    ``# If both the integers have``    ``# equal count of set bits``    ``if` `(cntA ``=``=` `cntB):``        ``return` `True``    ``return` `False` `# Driver code``a ``=` `3``b ``=` `9` `if` `(isPossible(a, b)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)` `# This code is contributed by Sanjit Prasad`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``    ` `    ``// recursive function to count set bits``    ``public` `static` `int` `countSetBits(``int` `n)``    ``{` `        ``// base case``        ``if` `(n == 0)``            ``return` `0;``        ``else` `            ``// if last bit set.Add 1 else.Add 0``            ``return` `(n & 1) + countSetBits(n >> 1);``    ``}``    ` `    ``// Function that returns true if the``    ``// binary representation of b can be``    ``// generated by permuting the``    ``// binary digits of a``    ``static` `bool` `isPossible(``int` `a, ``int` `b)``    ``{``    ` `        ``// Find the count of set bits``        ``// in both the integers``        ``int` `cntA = countSetBits(a);``        ``int` `cntB = countSetBits(b);``    ` `        ``// If both the integers have``        ``// equal count of set bits``        ``if` `(cntA == cntB)``            ``return` `true``;``        ``return` `false``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main (String[] args)``    ``{``        ``int` `a = 3, b = 9;``    ` `        ``if` `(isPossible(a, b))``            ``Console.WriteLine(``"Yes"``);``        ``else``            ``Console.WriteLine(``"No"``);``    ``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`Yes`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up