Count of distinct XORs formed by rearranging two Binary strings

Given two binary strings A and B of equal length N, the task is to find the number of distinct XORs possible by arbitrarily reordering the two binary strings. Since the number can be large enough, find the number modulo 109 + 7

Examples:

Input: A = “00”, B = “01”
Output: 2
Explanation:
There are two possible results by rearranging the digits of the string B.
They are: “10” and “01”

Input: A = “010”, B = “100”
Output: 4
Explanation:
There are four possible results possible by rearranging the digits of both the strings.
They are: “000”, “110”, “011”, “101”

Approach:



  1. Since, we know that
    0 XOR 0 = 0
    0 XOR 1 = 1
    1 XOR 0 = 1
    1 XOR 1 = 0
    

    Therefore, to get an XOR value as ‘1’ at any index of the result string, the input strings must have odd number of 1s at that index.

  2. Now, we will try to rearrange the Binary strings in a way, that maximum number of indices have an odd number of 1s at them. This can be visualized by the following example:
  3. Therefore, from the above observation, the idea is to find the minimum and the maximum number of 1’s possible by reordering the strings.
    • To find the Maximum ‘1’s: The maximum ‘1’s in the result would occur when maximum {0, 1} and {1, 0} pairs are formed. Therefore,

      Maximum number of {0, 1} pairs = minimum(count of ‘0’ in A, count of ‘1’ in B)
      Maximum number of {1, 0} pairs = minimum(count of ‘1’ in A, count of ‘0’ in B)

      Therefore, Maximum number of ‘1’s in the XOR = Maximum number of {0, 1} pairs + Maximum number of {1, 0} pairs

    • To find the Minimum ‘1’s: This case can be seen as the converse of the maximum number of ‘0’s in the result. Similarly, the maximum ‘0’s in the result would occur when maximum {0, 0} and {1, 1} pairs are formed. Therefore,

      Maximum number of {0, 0} pairs = minimum(count of ‘0’ in A, count of ‘0’ in B)
      Maximum number of {1, 1} pairs = minimum(count of ‘1’ in A, count of ‘1’ in B)
      Maximum number of ‘0’s in the XOR = Maximum number of {0, 0} pairs + Maximum number of {1, 1} pairs

      Therefore, Minimum number of ‘1’s in the XOR = N – Maximum number of ‘0’s in the XOR

  4. All the combinations of 1’s can be formed in between these two numbers (minimum and maximum) with the difference of 2.
  5. Finally, the total number of possible ways to get the result can be calculated by the number of combinations from the minimum number of 1’s and the maximum number of 1’s with a step of 2.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the number of
// distinct XORs formed by rearranging
// two binary strings
#include <bits/stdc++.h>
  
using namespace std;
  
// function to compute modulo power
long long power(long long a, long long b, long long mod)
{
    long long aa = 1;
    while(b)
    {
  
        if(b&1)
        {
            aa = aa * a;
            aa %= mod;
        }
        a = a * a;
        a %= mod;
        b /= 2;
    }
  
    return aa;
}
  
// Function to calculate nCr % p
// over a range
long long nCrRangeSum(long long n, long long r1, 
                    long long r2, long long p)
{
  
    // Initialize the numerator
    // and denominator
    long long num = 1, den = 1;
  
    // Initialize the sum
    long long sum = 0;
  
    // nC0 is 1
    if (r1 == 0)
        sum += 1;
  
    // Traversing till the range
    for (int i = 0; i < r2; i++)
    {
  
        // Computing the numerator
        num = (num * (n - i)) % p;
  
        // Computing the denominator
        den = (den * (i + 1)) % p;
  
        // If 'i' lies between the given range
        // and is at an even long long interval from
        // the starting range because
        // the combinations at a step of 2
        // is required
        if(i - r1 >= -1 and (i - r1 + 1) % 2 == 0)
        {
  
            // Computing nCr and adding the value
            // sum
            sum += (num * power(den, p - 2, p)) % p;
            sum %= p;
        }
        }
    return sum;
}
  
// Function to find the number of
// distinct XORs formed by
// rearranging two binary strings
int compute(string A, string B, int N)
{
  
    // Initializing the count variable
    // to 0
    int c0A = 0, c1A = 0, c0B = 0, c1B = 0;
  
    // Iterating through A
    for (char c:A) {
  
        // Increment the c1A variable
        // if the current element is 1
        if (c == '1')
            c1A += 1;
  
        // Increment the c0A variable
        // if the current element is 0
        else if (c == '0')
            c0A += 1;
        }
  
    // Iterating through B
    for (char c:B){
  
        // Increment the c1B variable
        // if the current element is 1
        if (c == '1')
            c1B += 1;
  
        // Increment the c0A variable
        // if the current element is 0
        else if (c == '0')
            c0B += 1;
        }
  
    // Finding the minimum number of '1's in the XOR
    // and the maximum number of '1's in the XOR
    int max1xor = min(c0A, c1B) + min(c1A, c0B);
    int min1xor = N - min(c0B, c0A) - min(c1A, c1B);
  
    // Compute the number of combinations between
    // the minimum number of 1's and the maximum
    // number of 1's and perform % with 10^9 + 7
    int ans = nCrRangeSum(N, min1xor, max1xor, 1000000000 + 7);
  
    // Return the answer
    return ans;
}
  
// Driver code
int main()
{
    long long N = 3;
    string A = "010";
    string B = "100";
      
    cout << compute(A, B,N);
    return 0;
}
  
// This code is contributed by mohit kumar 29

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to find the number of 
// distinct Bitwise XORs formed by rearranging 
// two binary strings 
  
class GFG 
{
    // function to compute modular exponentiation
    // i.e. to find (a^b) % mod 
    static long mod_power(long a, long b,
                          long mod)
    {
        long result = 1l;
        while(b > 0)
        {
            if((b&1) == 0) // b is even
            {
                result = a * a;
                a %= mod;
                b /= 2;
            }
            else // b is odd
            {
                result = result * a;
                result %= mod;
            }
        }
        return result;
    }
  
    // method to evaluate nCr modulo p
    // over an interval
    static long nCr_RangeSum(long n, long r1, 
                            long r2, long p)
    {
  
        // initializing numerator 
        // and denominator
        long num = 1, den = 1;
  
        // initialize the sum 
        long sum = 0l;
  
        // nC0 is 1
        if(r1 == 0)
            sum += 1l;
  
        // Iterating through the range
        for(int i = 0; i < r2; i++)
        {
  
            // computing the numerator
            num = (num * (n - i)) % p;
              
            // computing the denominator
            den = (den * (i + 1)) % p; 
  
            // If 'i' lies between the given range 
            // and is at an even interval from  
            // the starting range because  
            // the combinations at a step of 2  
            // is required 
            if(i - r1 >= -1 && (i - r1 + 1) % 2 == 0
            {
                // Computing nCr and adding the value 
                // to the sum   
                sum += (num * mod_power(den, p - 2, p)) % p; 
                sum %= p; 
            }
        }
        return sum;
    }
      
    // method to find the number of
    // distinct XORs formed by 
    // rearrangement of two binary strings
    static long compute(String A, String B, int N)
    {
        // Initializing the counter variables 
        // to 0 
        int c0A = 0, c1A = 0, c0B = 0, c1B = 0
  
        // Iterating through A's characters 
        for (char c : A.toCharArray()) 
        
  
            // Increment the c1A variable 
            // if the current element is 1 
            if (c == '1'
                c1A += 1
  
            // Increment the c0A variable 
            // if the current element is 0 
            else if (c == '0'
                c0A += 1
            
  
        // Iterating through B's characters 
        for (char c : B.toCharArray())
        
  
            // Increment the c1B variable 
            // if the current element is 1 
            if (c == '1'
                c1B += 1
  
            // Increment the c0A variable 
            // if the current element is 0 
            else if (c == '0'
                c0B += 1
        
        // Finding the minimum number of '1's in the XOR 
        // and the maximum number of '1's in the XOR 
        int max1xor = Math.min(c0A, c1B) + Math.min(c1A, c0B); 
        int min1xor = N - Math.min(c0B, c0A) - Math.min(c1A, c1B); 
  
        // Compute the number of combinations between 
        // the minimum number of 1's and the maximum 
        // number of 1's and perform modulo with 10^9 + 7 
        long ans =  nCr_RangeSum(N, min1xor, max1xor, 1000000000 + 7);
  
        // Return the answer 
        return ans; 
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int N = 3; // length of each string
          
        String A = "010"
        String B = "100"
          
        System.out.print(compute(A, B, N));
    }
}
  
// This Code is contributed by Soumitri Chattopadhyay.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the number of 
# distinct XORs formed by rearranging 
# two binary strings
  
# Function to calculate nCr % p 
# over a range
def nCrRangeSum(n, r1, r2, p):
  
    # Initialize the numerator 
    # and denominator 
    num = den = 1
  
    # Initialize the sum
    sum = 0
      
    # nC0 is 1
    if r1 == 0:
        sum += 1
  
    # Traversing till the range
    for i in range(r2):
  
        # Computing the numerator
        num = (num * (n - i)) % p
  
        # Computing the denominator 
        den = (den * (i + 1)) %
  
        # If 'i' lies between the given range
        # and is at an even interval from 
        # the starting range because 
        # the combinations at a step of 2 
        # is required 
        if(i - r1 >= -1 and (i - r1 + 1) % 2 == 0):
  
            # Computing nCr and adding the value 
            # sum
            sum += (num * pow(den, p - 2, p)) % p
            sum %= p
    return sum
  
# Function to find the number of 
# distinct XORs formed by 
# rearranging two binary strings
def compute(A, B):
  
    # Initializing the count variable
    # to 0
    c0A = c1A = c0B = c1B = 0
  
    # Iterating through A
    for c in A:
  
        # Increment the c1A variable
        # if the current element is 1
        if c == '1':
            c1A += 1
  
        # Increment the c0A variable
        # if the current element is 0
        elif c == '0':
            c0A += 1
  
    # Iterating through B
    for c in B:
  
        # Increment the c1B variable
        # if the current element is 1
        if c == '1':
            c1B += 1
  
        # Increment the c0A variable
        # if the current element is 0
        elif c == '0':
            c0B += 1
  
    # Finding the minimum number of '1's in the XOR
    # and the maximum number of '1's in the XOR
    max1xor = min(c0A, c1B) + min(c1A, c0B)
    min1xor = N - min(c0B, c0A) - min(c1A, c1B)
      
    # Compute the number of combinations between
    # the minimum number of 1's and the maximum
    # number of 1's and perform % with 10^9 + 7
    ans = nCrRangeSum(N, min1xor, max1xor, 10**9 + 7)
  
    # Return the answer
    return ans
  
# Driver code
if __name__ == "__main__":
  
    N = 3
    A = "010"
    B = "100"
  
    print(compute(A, B))

chevron_right


Output:

4

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.




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.