ALternate Binary String

I am having a doubt in the question named alternating binary string that came in starters 130
#include<bits/stdc++.h>
using namespace std;
define ll long long
int main()
{
int t;
cin >> t;
while (t–) {
int n;
cin >> n;
string s;
cin >> s;
ll count = 0;
ll consec = 0;
for (int i = 0; i < n; i++) {
if (s[i] == ‘0’) {
consec++;
} else {
count += ((consec) / 2);
consec = 0;
}
}
ll consec1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == ‘1’) {
consec1++;
} else {
count += ((consec1) / 2);
consec1 = 0;
}
}

        count += ((consec1) / 2);

        count += ((consec) / 2);

    cout << count << endl;
}
return 0;

}
My code is this i couldnt think of where my code fails. here’s the link of the question Alternating Binary String Practice Coding Problem - CodeChef.

Your code assumes that you only need 1 operation to flip any element, which is wrong.

You need 2 operations to make “000” into “010”

  1. 001
  2. 010

The operaion flips all the elements after the index you choose. You can not flip exclusively the index you want. Your code only counts the indexes that need to be flipped, but not how many operations are needed.

1 Like

okay got my mistake, thanks a lot buddy.

1 Like