Help me in solving XOR_PAL problem

My issue

After counting the number of different pairs of bits in the string I don’t understand how it goes that answer is (count+1)/2.

My code

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int count=0;
        for(int i=1;i<=n/2;i++)
        {
            if(s[i]!=s[n-i+1])
            {
                count++;
            }
        }
        cout<<(count+1)/2<<endl;
    }
}

Problem Link: Xor Palindrome Practice Coding Problem

let the testcase be
5
00011
this can be converted into palindrome using 1 operation
1^1=0;
00000
let another test case be
5
10011
this can be converted into palindrome using 1 operation
you are using (cnt+1)/2
if there are odd number of cnt then ans will be cnt/2 + 1
if there are even number of cnt then ans will be cnt/2

1 Like

(count+1)/2 gives count/2 if count is even
else it gives count/2 +1 if count is odd

1 Like

I got it.
Thanks

No need for thanks just like the reply :laughing:

1 Like