Number of ways to change the XOR of two numbers by swapping the bits
Given two binary strings s1 and s2. The XOR of them is X, the task is to find the number of ways to swap two-bit positions in string s1 such that XOR formed between new s1 and s2 is not same as X.
Examples:
Input: s1 = “01011”, s2 = “11001”
Output: 4
swap bits of index(1-based) (1, 4), (2, 3), (3, 4), or (3, 5) such that XOR value is changed.
Input: s1 = “011000”, s2 = “010011”
Output: 6
Approach:
- Count the number of 1’s and 0’s in s1.
- Traverse in the string s1, and check for two cases:
- 0 and 0 in s1[i] and s2[i], as replacing 0 with 1, will change the XOR value.
- 1 and 0 in s1[i] and s2[i], as replacing 1 with 0 will change the XOR value.
- For the first case, the number of ways of replacement will be the number of ones-already used 1’s.
- For the second case, the number of ways of replacement will be the number of zeros-already used 0’s.
- summation of number of ways in both the cases will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays(string s1, string s2)
{
int c1 = 0, c0 = 0;
int n = s1.length();
for ( int i = 0; i < n; i++) {
if (s1[i] == '1' )
c1++;
else
c0++;
}
int used1 = 0, used0 = 0;
int ways = 0;
for ( int i = 0; i < n; i++) {
if (s1[i] == '0' and s2[i] == '0' ) {
ways += c1;
ways -= used1;
used0++;
}
else if (s1[i] == '1' and s2[i] == '0' ) {
ways += c0;
ways -= used0;
used1++;
}
}
return ways;
}
int main()
{
string s1 = "01011" ;
string s2 = "11001" ;
cout << countWays(s1, s2);
return 0;
}
|
Java
class GFG
{
static int countWays(String s1,
String s2)
{
int c1 = 0 , c0 = 0 ;
int n = s1.length();
for ( int i = 0 ; i < n; i++)
{
if (s1.charAt(i) == '1' )
c1++;
else
c0++;
}
int used1 = 0 , used0 = 0 ;
int ways = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s1.charAt(i) == '0' &&
s2.charAt(i) == '0' )
{
ways += c1;
ways -= used1;
used0++;
}
else if (s1.charAt(i) == '1' &&
s2.charAt(i) == '0' )
{
ways += c0;
ways -= used0;
used1++;
}
}
return ways;
}
public static void main(String[] args)
{
String s1 = "01011" ;
String s2 = "11001" ;
System.out.println(countWays(s1, s2));
}
}
|
Python3
def countWays(s1, s2):
c1 = 0
c0 = 0
n = len (s1)
for i in range ( 0 ,n) :
if (s1[i] = = '1' ):
c1 + = 1
else :
c0 + = 1
used1 = 0
used0 = 0
ways = 0
for i in range ( 0 ,n) :
if (s1[i] = = '0' and s2[i] = = '0' ) :
ways + = c1
ways - = used1
used0 + = 1
elif (s1[i] = = '1' and s2[i] = = '0' ) :
ways + = c0
ways - = used0
used1 + = 1
return ways
if __name__ = = '__main__' :
s1 = "01011"
s2 = "11001"
print (countWays(s1, s2))
|
C#
using System;
class GFG
{
static int countWays(String s1,
String s2)
{
int c1 = 0, c0 = 0;
int n = s1.Length;
for ( int i = 0; i < n; i++)
{
if (s1[i] == '1' )
c1++;
else
c0++;
}
int used1 = 0, used0 = 0;
int ways = 0;
for ( int i = 0; i < n; i++)
{
if (s1[i] == '0' &&
s2[i] == '0' )
{
ways += c1;
ways -= used1;
used0++;
}
else if (s1[i] == '1' &&
s2[i] == '0' )
{
ways += c0;
ways -= used0;
used1++;
}
}
return ways;
}
public static void Main(String[] args)
{
String s1 = "01011" ;
String s2 = "11001" ;
Console.WriteLine(countWays(s1, s2));
}
}
|
PHP
<?php
function countWays( $s1 , $s2 )
{
$c1 = 0;
$c0 = 0;
$n = strlen ( $s1 );
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s1 [ $i ] == '1' )
$c1 ++;
else
$c0 ++;
}
$used1 = 0;
$used0 = 0;
$ways = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s1 [ $i ] == '0' and
$s2 [ $i ] == '0' )
{
$ways += $c1 ;
$ways -= $used1 ;
$used0 ++;
}
else if ( $s1 [ $i ] == '1' and
$s2 [ $i ] == '0' )
{
$ways += $c0 ;
$ways -= $used0 ;
$used1 ++;
}
}
return $ways ;
}
$s1 = "01011" ;
$s2 = "11001" ;
echo countWays( $s1 , $s2 );
?>
|
Javascript
<script>
function countWays(s1, s2)
{
let c1 = 0, c0 = 0;
let n = s1.length;
for (let i = 0; i < n; i++) {
if (s1[i] == '1' )
c1++;
else
c0++;
}
let used1 = 0, used0 = 0;
let ways = 0;
for (let i = 0; i < n; i++) {
if (s1[i] == '0' && s2[i] == '0' ) {
ways += c1;
ways -= used1;
used0++;
}
else if (s1[i] == '1' && s2[i] == '0' ) {
ways += c0;
ways -= used0;
used1++;
}
}
return ways;
}
let s1 = "01011" ;
let s2 = "11001" ;
document.write(countWays(s1, s2));
</script>
|
Output:
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...