simple ^^
1 pass / 3 variables\ntime: O(N); space: O(1)\n\nIntro:\n\nThe useful observation is that the strings "0101010....." and "1010101....." are "complementary" and 
**1 pass / 3 variables**\n**time: `O(N)`; space: `O(1)`**\n\n**Intro:**\n\nThe useful observation is that the strings `"0101010....."` and `"1010101....."` are "complementary" and the number of mismatches with `"0101010....."` is equal to the number of matches with `"1010101....."` and vice versa. So there\'s no point to count matches with both strings. Also the sum of matches and mismatches with either  of the strings equals the size of the input array. So you can just count the number of matches with any of two strings and derive all the other necessary values. In the presented algo I choose to count the **difference** of matches and mismatches with `"101010..."` though.\n\n![image](https://assets.leetcode.com/users/images/5d5e54de-b144-4334-9695-80283be78d96_1660499287.461111.png)\n\n**Algo**\n\n* traverse the input array, keep `d` quantity up to date and keep track of its maximum(`M`) and minimum(`m`). If `s[i]` matches with the `ith` character of `"1010101010..."` string then `d` gets `+1`, otherwise it gets `-1`. So basically `d` is a difference of matches of `s` with  string `"01010101..."` and string `"1010101..."`. If you know final value of `d`then it\'s easy to get the number of matches  with the strings`"01010101..."` or `"1010101..."`: it\'s `(size(s)-d)/2` and `(size(s)+d)/2` respectively.\n\n* judging by the parity of the size of the input array use one of two expressions for the `return` that involve final value of `d`,`m`,`M` and `size(s)`.\n\n```\nint minFlips(string s) \n{\n\tint d{}, m{INT_MAX}, M{INT_MIN}, sz(size(s));\n\tfor(int i{}; i<sz; ++i)\n\t{\n\t\td += (i&1)+\'0\'!=s[i] ? 1 : -1; \n\t\tm  = min(m, d);\n\t\tM  = max(M, d);\n\t}  \n\treturn sz&1 ? min((sz-d)/2+m, (sz+d)/2-M) : min((sz-d)/2, (sz+d)/2);\n}\n```\n**Notation:**\n\n`d` stands for **d**elta of matches of `s` with `"01010101..."` and `"1010101..."`;\n`m` - **m**inimum of delta;\n`M` - **m**aximum of delta;\n`sz` - **s**i**z**e of the input array.\n\n**Explanation:**\n\nLet\'s discuss the differences between arrays with even and odd sizes.\nThe truth is that there\'s absolutely no point in performing type 1 operation for even arrays. You just calculate the number of matches with strings `"01010101..."` and `"1010101..."` and return the least of them. This picture can explain why type 1 operations are useless in diminishing the number of type 2 operations.\n\n![image](https://assets.leetcode.com/users/images/1fdd8fda-8d3a-4a9d-809c-a4fc3c5c8aed_1660501176.4529552.png)\n\nOn the other hand if the input array is odd you can basically invert some number of leading array values and after that choose the minimum of matches with strings `"01010101..."` and `"101010..."`. \n\n![image](https://assets.leetcode.com/users/images/5c4c5a80-30bf-40a1-8983-72e10bc6c6d2_1660502014.6485038.png)\n\nIt turns out that you may consider inverting up to a minimum or a maximum of `d`. In the first case the maximum number of matches will be with string `"101010..."`, while in the second case the string `"01010101..."` is a better match.\n\n![image](https://assets.leetcode.com/users/images/aef57be7-304a-4f02-996a-d7cb06aa45c8_1660524465.1682947.png)\n\n\n
['C', 'C++']