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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.