# Getting a WA in DEVSTR

I was trying to solve this problem. I am getting a WA in all cases except case no. 2. My solution is like the one in the editorial. Here is a link to my submissions. Please help. Thanks.

Here’s my code:

``````#include <iostream>
#include <iomanip>

#include <cmath>
#include <string>

#include <algorithm>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int main() {
int t, n, k;
string input;
cin >> t;

while(t--) {
cin >> n >> k;
cin.ignore();
getline(cin, input);

if (k == 1) {
int z = 0, o = 0;
for (int i = 0; i < n; i++) {
if (i % 2) {
if (input[i] == '0') z++;
else if (input[i] == '1') o++;
}
else {
if (input[i] == '0') o++;
else if (input[i] == '1') z++;
}
}

if (o > z) {
cout << z << endl;
for (int i = 0; i < n; i++) cout << static_cast<char>((i%2) + 48);
cout << endl;
}
else {
cout << o << endl;
for (int i = 0; i < n; i++) cout << static_cast<char>(((i+1)%2) + 48);
cout << endl;
}
continue;
}

int prev = 0;
char c = input[0];
int total = 0;

for (int i = 1; i < n; i++) {
if ((input[i] != c) || (i == n - 1)) {
if (i == n - 1) i = n;
total += floor(double(i - prev)/(k+1));

int start = prev + k;
if (((i - prev) % (k+1) == 0) && (i != n)) {
start--;
}

for (int j = start; j < i; j+=k+1) {
input[j] = static_cast<char>((c - 47) % 2 + 48);
}

prev = i;
c = input[i];
}
}
cout << total << endl;
cout << input << endl;
}

return 0;
}
``````

@dhruvsomani didn’t check your logic but your code outputs 2 for the following test case, although the correct answer is 1.

8 2

11111001

1 Like

Thanks. I’ll try to correct it.

1 Like

Thanks again. Got an AC.

1 Like