Recurring observations about arrays and strings that turn “simulate the operations” into one scan.

SettingObservation
binary strings, flip / reverse operationscount adjacent equal pairs or the number of runs; each operation changes it by a bounded amount
make a string alternatingcompare against both targets 0101... and 1010...
rearrange into a palindromepossible iff at most one character has odd count
remove a character to make a palindrometwo-pointer from both ends; at the first mismatch try skipping either side
MEX of subarraysMEX of the whole array is an upper bound; subarrays containing all of have MEX
“delete equal adjacent pairs”stack simulation; the result is unique (like a group reduction)
prefix sums with a twistanswer queries by the parity / sign of prefix sums
periodic stringsthe smallest period is from the prefix function

Reversals to an alternating string

A binary string of even length with equally many 0s and 1s; one operation reverses any substring. The minimum number of operations to make it alternating:

int reversalsToAlternate(const string& s) {
    int z = 0, o = 0;
    for (int i = 0; i + 1 < sz(s); i++) if (s[i] == s[i + 1]) (s[i] == '0' ? z : o)++;
    return max(z, o);
}

Pitfalls

  • Strings with odd length, or unequal counts, make a target impossible: check the counts first.
  • “Subsequence” vs “substring”: a subsequence operation is usually much more powerful (often the answer depends only on counts).
  • For palindrome problems, the middle character of an odd-length string is unconstrained.

Related: Subarray Aggregates and MEX, Palindromes, Runs and Periodicity.