Minimize insertion of 0 or 1 such that no adjacent pair has same value
Last Updated :
31 Mar, 2023
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum number of
// operations required to make
// consecutive element different in
// an array
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;
}
// Driver Code
int main()
{
int A[] = { 0, 0, 1, 0, 0 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << minOperation(A, N);
return 0;
}
// This code is contributed by aarohirai2616.
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find minimum number of
// operations required to make
// consecutive element different in
// an array
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;
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 0, 0, 1, 0, 0 };
int N = A.length;
// Function Call
System.out.println(minOperation(A, N));
}
}
Python3
# Python code to implement the approach
# Function to find minimum number of
# operations required to make
# consecutive element different in
# an array
def minOperation(arr, n):
count = 0
for i in range(0, n-1):
if (arr[i] == arr[i + 1]):
count = count + 1
return count
# Driver Code
if __name__ == '__main__':
A = [0, 0, 1, 0, 0]
N = len(A)
# Function call
print(minOperation(A, N))
# This code is contributed by aarohirai2616.
C#
// C# code to implement the approach
using System;
public class GFG{
// Function to find minimum number of
// operations required to make
// consecutive element different in
// an array
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 (){
// Code
int[] A = { 0, 0, 1, 0, 0 };
int N = A.Length;
// Function Call
Console.WriteLine(minOperation(A, N));
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// Javascript code to implement the approach
// Function to find minimum number of
// operations required to make
// consecutive element different in
// an array
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;
}
// Driver Code
let A = [ 0, 0, 1, 0, 0 ];
let N = A.length;
// Function call
console.log(minOperation(A, N));
// This code is contributed by Samim Hossain Mondal.
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum inversions required so that no two adjacent elements are same Given a binary array arr[] of size N. The task is to find the minimum number of inversions required so that no two adjacent elements are same. After a single inversion, an element could change from 0 to 1 or from 1 to 0.Examples: Input: arr[] = {1, 1, 1} Output: 1 Change arr[1] from 1 to 0 and the a
5 min read
Ways to form n/2 pairs such that difference of pairs is minimum Given an array arr of N integers, the task is to split the elements of the array into N/2 pairs (each pair having 2 elements) such that the absolute difference between two elements of any pair is as minimum as possible. Note: N will always be even. Examples: Input: arr[] = {1, 7, 3, 8} Output: 1 The
15+ min read
Minimize cost of increments or decrements such that same indexed elements become multiple of each other Given two arrays A[] and B[] consisting of N integers, the task is to minimize the total cost of incrementing or decrementing array elements by 1 such that for every ith element, either A[i] is a multiple of B[i] or vice-versa. Examples: Input: A[] = {3, 6, 3}, B[] = {4, 8, 13}Output: 4Explanation:I
6 min read
Minimum count of 0s to be selected such that all 1s are adjacent to them Given a binary string str of size N whose every character is either '1' or '0'. The task is to select minimum number of 0's such that at least one neighbor for every '1' is selected. Print the count of the selected 0's. Examples: Input: str = "1001"Output: 2Explanation: '0's can be selected from ind
5 min read
Closest pair in an Array such that one number is multiple of the other Given an array arr[] of integers of size N, the task is to find the closest pair in the given array such that one element is the multiple of the other. If no such pair exists then print -1. Note: Closest pair means the difference between the index of any two elements must be minimum. Examples: Input
6 min read