Open In App

Check if B can be formed by permuting the binary digits of A

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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
import java.io.*;
public 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




<script>
    // Javascript implementation of the approach
     
    // recursive function to count set bits 
    function countSetBits(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 
    function isPossible(a, b) 
    
       
        // Find the count of set bits 
        // in both the integers 
        let cntA = countSetBits(a); 
        let cntB = countSetBits(b); 
       
        // If both the integers have 
        // equal count of set bits 
        if (cntA == cntB) 
            return true
        return false
    
     
    let a = 3, b = 9; 
       
    if (isPossible(a, b)) 
      document.write("Yes"); 
    else
      document.write("No"); 
 
// This code is contributed by divyesh072019.
</script>


Output: 

Yes

 

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads