Count of distinct XORs formed by rearranging two Binary strings
Last Updated :
20 Mar, 2023
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:
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.
- 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:

- 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
- All the combinations of 1's can be formed in between these two numbers (minimum and maximum) with the difference of 2.
- 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:
C++14
// C++ 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
Java
// 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.
Python3
# Python3 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)) % 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 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))
C#
// C# program to find the number of
// distinct Bitwise XORs formed by
// rearranging two binary strings
using System;
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 = 1;
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;
}
// Function 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 = 0;
// nC0 is 1
if (r1 == 0)
sum += 1;
// 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;
}
// Function 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
foreach(char 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
else if (c == '0')
c0A += 1;
}
// Iterating through B's characters
foreach(char 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
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
static public void Main()
{
// Length of each string
int N = 3;
string A = "010";
string B = "100";
Console.WriteLine(compute(A, B, N));
}
}
// This code is contributed by offbeat
JavaScript
// JavaScript program to find the number of
// distinct XORs formed by rearranging
// two binary strings
// function to compute modulo power
function power(a, b, mod)
{
var aa = 1n;
while (b) {
if (BigInt(b) & 1n) {
aa = aa * a;
aa %= mod;
}
a = a * a;
a %= mod;
b = Math.floor(Number(BigInt(b) / 2n));
}
return aa;
}
// Function to calculate nCr % p
// over a range
function nCrRangeSum(n, r1, r2, p)
{
// Initialize the numerator
// and denominator
var num = 1n;
var den = 1n;
// Initialize the sum
var sum = 0n;
// nC0 is 1
if (r1 == 0)
sum += 1n;
// Traversing till the range
for (var i = 0; i < r2; i++) {
// Computing the numerator
num = (num * (BigInt(n) - BigInt(i))) % p;
// Computing the denominator
den = BigInt(den * (BigInt(i) + 1n)) % 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 && (i - r1 + 1) % 2 == 0) {
// Computing nCr and adding the value
// sum
sum += BigInt(num * power(den, p - 2n, p)) % p;
sum %= p;
}
}
return Number(sum);
}
// Function to find the number of
// distinct XORs formed by
// rearranging two binary strings
function compute(A, B, N)
{
// Initializing the count variable
// to 0
var c0A = 0;
var c1A = 0;
var c0B = 0;
var c1B = 0;
// Iterating through A
for (var c of 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 (var c of 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
var max1xor = Math.min(c0A, c1B) + Math.min(c1A, c0B);
var 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 % with 10^9 + 7
var ans = nCrRangeSum(N, min1xor, max1xor,
BigInt(1000000000 + 7));
// Return the answer
return ans;
}
// Driver code
var N = 3;
var A = "010";
var B = "100";
// Function call
console.log(compute(A, B, N));
// This code is contributed by phasing17
Time Complexity : O( N )
Space Complexity : O( 1 )
Similar Reads
Count of possible distinct Binary strings after replacing "11" with "0"
Given a binary string str of size N containing 0 and 1 only, the task is to count all possible distinct binary strings when a substring "11" can be replaced by "0". Examples: Input: str = "11011"Output: 4Explanation: All possible combinations are "11011", "0011", "1100", "000". Input: str = "1100111
15 min read
Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties
Given a K and a matrix Q[][] consisting of queries of the form {N, M}, the task for each query is to count the number of strings possible of all lengths from Q[i][0] to Q[i][1] satisfying the following properties: The frequency of 0's is equal to a multiple of K.Two strings are said to be different
6 min read
Count of distinct substrings of a string using Suffix Array
Given a string of length n of lowercase alphabet characters, we need to count total number of distinct substrings of this string. Examples: Input : str = âababaâ Output : 10 Total number of distinct substring are 10, which are, "", "a", "b", "ab", "ba", "aba", "bab", "abab", "baba" and "ababa"Reco
15+ min read
Count Number of Distinct Binary Strings After Applying Flip Operations
Given a binary string s and a positive integer k. In a operation you can repeatedly choose any substring of length k in s and flip all its characters (0s to 1s, 1s to 0s). The task is to return the number of distinct strings that can be obtained, modulo 10^9 + 7. You can perform the flip operation a
5 min read
Count of operations to make a binary string"ab" free
Given a string containing characters 'a' and 'b' only. Convert the given string into a string in which there is no 'ab' substring. To make string 'ab' free we can perform an operation in which we select a 'ab' substring and replace it by 'bba'. Find the total number of operations required to convert
5 min read
Rearrange given binary strings to maximize their Bitwise XOR value
Given three binary strings S1, S2, and S3 each of length N, the task is to find the maximum possible Bitwise XOR that can be obtained by rearranging the characters of the given strings. Examples: Input: S1 = "1001", S2 = "0010", S3 = "1110"Output: 15Explanation:Rearrange the digits of S1 as "1010",
15+ min read
Find the minimum number of distinct Substrings formed by 0s in Binary String
Given two integers N and M, the task is to output the minimum number of different substrings that can be formed using the Binary String of length N having M number of set bits. Note: Two substrings let say S[x, y] and S[a, b] are different. If either x != a or y != b. Examples: Input: N = 3, M = 1Ou
6 min read
Count of distinct strings that can be obtained after performing exactly one swap
Given a string s containing lowercase English alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. Input: s = "geek"Output: 6Explanation: Following are the strings formed by doing exactly one swapstrings = ["egek","eegk"
5 min read
Split Strings into two Substrings whose distinct element counts are equal
Given a string S of length N, the task is to check if a string can be split into two non-intersecting strings such that the number of distinct characters are equal in both. Examples: Input: N = 6, S = "abccba"Output: TrueExplanation: Splitting two strings as "abc" and "cba" has 3 distinct characters
8 min read
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
Given an array string[] consisting of N numeric strings of length M, the task is to find the number of distinct strings that can be generated by selecting any two strings, say i and j from the array and swap all possible prefixes between them. Note: Since the answer can be very large, print modulo 1
6 min read