Minimize insertion of 0 or 1 such that no adjacent pair has same value
Given a binary array A[] of length N, the task is to find the minimum number of operations required such that no adjacent pair has the same value where in each operation we can insert either 0 or 1 at any position in the array.
Examples:
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
Approach: The problem can be solved based on the following observation:
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.
Follow the below steps to solve the problem:
- Initialize a variable count = 0.
- Iterate a loop for each element in A, and check if it is equal to the next element.
- If yes, increment the count by 1.
- Print the count which gives the minimum number of operations required to make consecutive elements different in an array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minOperation( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1]) {
count++;
}
}
return count;
}
int main()
{
int A[] = { 0, 0, 1, 0, 0 };
int N = sizeof (A) / sizeof (A[0]);
cout << minOperation(A, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int minOperation( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (arr[i] == arr[i + 1 ]) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [] A = { 0 , 0 , 1 , 0 , 0 };
int N = A.length;
System.out.println(minOperation(A, N));
}
}
|
Python3
def minOperation(arr, n):
count = 0
for i in range ( 0 , n - 1 ):
if (arr[i] = = arr[i + 1 ]):
count = count + 1
return count
if __name__ = = '__main__' :
A = [ 0 , 0 , 1 , 0 , 0 ]
N = len (A)
print (minOperation(A, N))
|
C#
using System;
public class GFG{
public static int minOperation( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1]) {
count++;
}
}
return count;
}
static public void Main (){
int [] A = { 0, 0, 1, 0, 0 };
int N = A.Length;
Console.WriteLine(minOperation(A, N));
}
}
|
Javascript
function minOperation(arr, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1]) {
count += 1;
}
}
return count;
}
let A = [ 0, 0, 1, 0, 0 ];
let N = A.length;
console.log(minOperation(A, N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
31 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...