Ways to remove one element from a binary string so that XOR becomes zero
Last Updated :
25 Apr, 2023
Given a binary string, task is to erase exactly one integer in the array so that the XOR of the remaining numbers is zero. The task is to count number of ways to remove one element so that XOR of that string become ZERO.
Examples :
Input : 10000
Output : 1
We only have 1 ways to
Input : 10011
Output : 3
There are 3 ways to make XOR 0. We
can remove any of the three 1's.
Input : 100011100
Output : 5
There are 5 ways to make XOR 0. We
can remove any of the given 0's
A simple solution is to one by one remove an element, then compute XOR of remaining string. And count occurrences where removing an element makes XOR 0.
An efficient solution is based on following fact. If count of 1s is odd, then we must remove a 1 to make count 0 and we can remove any of the 1s. If count of 1s is even, then XOR is 0, We can remove any of the 0s and XOR will remain 0.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int xorZero(string str)
{
int one_count = 0, zero_count = 0;
int n = str.length();
for ( int i = 0; i < n; i++)
if (str[i] == '1' )
one_count++;
else
zero_count++;
if (one_count % 2 == 0)
return zero_count;
return one_count;
}
int main()
{
string str = "11111" ;
cout << xorZero(str) << endl;
return 0;
}
|
Java
import java.util.*;
class CountWays
{
static int xorZero(String s)
{
int one_count = 0 , zero_count = 0 ;
char [] str=s.toCharArray();
int n = str.length;
for ( int i = 0 ; i < n; i++)
if (str[i] == '1' )
one_count++;
else
zero_count++;
if (one_count % 2 == 0 )
return zero_count;
return one_count;
}
public static void main(String[] args)
{
String s = "11111" ;
System.out.println(xorZero(s));
}
}
|
Python3
def xorZero( str ):
one_count = 0
zero_count = 0
n = len ( str )
for i in range ( 0 , n, 1 ):
if ( str [i] = = '1' ):
one_count + = 1
else :
zero_count + = 1
if (one_count % 2 = = 0 ):
return zero_count
return one_count
if __name__ = = '__main__' :
str = "11111"
print (xorZero( str ))
|
C#
using System;
class GFG
{
static int xorZero( string s)
{
int one_count = 0,
zero_count = 0;
int n = s.Length;
for ( int i = 0; i < n; i++)
if (s[i] == '1' )
one_count++;
else
zero_count++;
if (one_count % 2 == 0)
return zero_count;
return one_count;
}
public static void Main()
{
string s = "11111" ;
Console.WriteLine(xorZero(s));
}
}
|
PHP
<?php
function xorZero( $str )
{
$one_count = 0; $zero_count = 0;
$n = strlen ( $str );
for ( $i = 0; $i < $n ; $i ++)
if ( $str [ $i ] == '1' )
$one_count ++;
else
$zero_count ++;
if ( $one_count % 2 == 0)
return $zero_count ;
return $one_count ;
}
$str = "11111" ;
echo xorZero( $str ), "\n" ;
?>
|
Javascript
<script>
function xorZero(s)
{
let one_count = 0, zero_count = 0;
let n = s.length;
for (let i = 0; i < n; i++)
if (s[i] == '1' )
one_count++;
else
zero_count++;
if (one_count % 2 == 0)
return zero_count;
return one_count;
}
let s = "11111" ;
document.write(xorZero(s));
</script>
|
Time complexity : O(n) //since one traversal of the String is required to complete all operations hence overall time required by the algorithm is linear
Auxiliary Space : O(1) // since no extra array is used so the space taken by the algorithm is constant
Share your thoughts in the comments
Please Login to comment...