#K9706. Minimum Operations to Achieve Alternating Binary Sequence
Minimum Operations to Achieve Alternating Binary Sequence
Minimum Operations to Achieve Alternating Binary Sequence
You are given a binary string S of length N. An alternating binary sequence is one in which no two adjacent bits are the same. In one operation, you can change a single bit (from '0' to '1' or vice versa). Your task is to determine the minimum number of operations required to transform the given string into an alternating binary sequence.
Consider the two possible alternating patterns:
- Pattern 1: Starts with 0, i.e. 0, 1, 0, 1, ...
- Pattern 2: Starts with 1, i.e. 1, 0, 1, 0, ...
You need to compute the number of positions at which the given string differs from each pattern. Formally, let the two patterns be \(P_1\) and \(P_2\). The minimum number of operations is given by:
[ \min\Bigg(\sum_{i=0}^{N-1} [S[i] \neq P_1[i]], ; \sum_{i=0}^{N-1} [S[i] \neq P_2[i]]\Bigg) ]
Output this minimum value.
inputFormat
The input is provided via standard input and consists of two lines:
- The first line contains a single integer N (1 \(\le\) N \(\le\) 105), representing the length of the binary string.
- The second line contains the binary string S of length N.
outputFormat
Output a single integer to standard output representing the minimum number of operations required to convert the provided binary string into an alternating binary sequence.
## sample5
11100
2
</p>