Input: A[] = {0, 0, 1, 0, 0}
Output: 2
?Explanation: We can perform the following operations to make consecutive element different in an array:
Insert 1 at index 1 in A = {0, 0, 1, 0, 0} ? {0, 1, 0, 1, 0, 0}
Insert 1 at index 5 in A = {0, 1, 0, 1, 0, 0} ? {0, 1, 0, 1, 0, 1, 0} all consecutive elements are different.
Input: A[] = {0, 1, 1}
Output: 1
A single move allows us to ‘break apart’ exactly one pair of equal adjacent elements of an array, by inserting either 1 between 0, 0 or 0 between 1, 1.
So, the answer is simply the number of pairs that are already adjacent and equal, i.e, positions i (1 ? i <N) such that Ai = Ai + 1, which can be computed with a simple for loop.
Below is the implementation of the above approach.