Problem
You are given a binary string A of length N.
You can perform the following type of operation on the string A:
- Choose two different indices \$i\$ and \$j\$ (\$1 \le i\$, \$j \le N\$)
- Change \$A_i\$ and \$A_j\$ to \$Ai \oplus Aj\$. Here \$\oplus\$ represents the bitwise XOR operation.
Input
A binary string consisting of 0's and 1's
Output
The minimum number of operations required to make the binary string a palindrome
Reference
Here's the link of the problem.
Actual Doubt
I tried solving it by the logic that the number of operations required would be equal to the number of inequalities of characters in the string when traversing from left and right. simultaneously.
My code:
for(int i=0,j=n-1;i<n/2;i++,j--) {
if(b[i]!=b[j]) count++;
}
Where b is the binary string and n is it's length.
But, it turns out the solution is this:
for(int i=0,j=n-1;i<n/2;i++,j--) {
if(b[i]!=b[j]) count++;
}
cout<<(count+1)/2<<endl;
And I don't understand why.
Can someone explain this? Thanks.