Minimum Number of Flips
Given a binary string (containing only 0s and 1s). We need to make this string a sequence of alternate characters (0 and 1) by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Write a program to determine the minimum number of bits to reach the goal
Examples:
Input:
0000 (binary string)
Output:
2
Input:
110 (binary string)
Output:
1
Example explanations:
Input:
0000
Output strings possible from the input string,
which satisfies the constraints:
0101
1010
Both strings are valid and both require 2 bits to be flipped.
Thus minimum no of flipping needed is 2
Input:
110
Output strings possible from the input string,
which satisfies the constraints:
010
101
The first one needs only one flips
While the second one need two flips
Hence minimum flip required is 1
Algorithm:
1) Declare variables and initialize like below.
2) Count number of flips needed for converting input string to the valid string starting with 0
3) Count number of flips needed for converting input string to the valid string starting with 1
4) return minimum of flip0 and flip1;
C++ implementation
Output
need an explanation for this answer? contact us directly to get an explanation for this answer