#C1516. Minimum Flips to Form an Alternating Array
Minimum Flips to Form an Alternating Array
Minimum Flips to Form an Alternating Array
You are given an array of binary digits (0s and 1s). Your task is to determine the minimum number of flips needed to transform the array into an alternating sequence. An alternating array is one where no two adjacent values are the same, and there are two possible patterns: starting with 0 or starting with 1.
For an index \( i \) (0-indexed), the expected value for the alternating patterns are:
- Pattern 1 (starting with 0): \( a_i = i \mod 2 \)
- Pattern 2 (starting with 1): \( a_i = 1 - (i \mod 2) \)
Your goal is to compute the minimum number of flips required between these two potential patterns.
Note: A flip means changing a 0 to 1 or a 1 to 0.
inputFormat
The input is given via stdin and consists of two lines:
- The first line contains a single integer \( n \) representing the length of the array.
- The second line contains \( n \) space-separated integers (each being either 0 or 1) representing the array.
outputFormat
Output a single integer to stdout which is the minimum number of flips required to convert the array into an alternating array.
## sample5
1 0 1 0 1
0