-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternatingbinarystring.c++
More file actions
43 lines (33 loc) · 965 Bytes
/
alternatingbinarystring.c++
File metadata and controls
43 lines (33 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
int minFlipsToAlternating(string S, int i) {
int flips = 0;
// Handle the starting character (avoid unnecessary flips at i)
if (i == 0 && S[0] == '1') {
flips++; // Flip the first character if it's 1
}
// Iterate through the string starting from position 'i+1'
// (because we already handled the character at 'i')
for (int j = i + 1; j < S.length(); ++j) {
// If the current position should have a different character than what's there
if ((j - i) % 2 == 0 && S[j] != '0') {
++flips; // Increment flips needed
} else if ((j - i) % 2 != 0 && S[j] != '1') {
++flips; // Increment flips needed
}
}
return flips;
}
int main() {
int n;
cin >> n;
for(int j = 0; j < n; ++j) {
string S;
cin >> S;
int i;
cin >> i; // Input the particular position 'i'
int min_flips = minFlipsToAlternating(S, i);
cout << min_flips << endl;
}
return 0;
}