Given a binary array arr[] consisting of equal count of 0s and 1s, the task is to count the minimum number of subarray reversal operations required to make the binary array alternating. In each operation reverse any subarray of the given array.
Examples:
Input: arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 }
Output: 2
Explanation:
Reversing the subarray {arr[1], …, arr[5]} modifies arr[] to { 1, 0, 1, 0, 1, 1, 0, 0 }
Reversing the subarray {arr[5], arr[6]} modifies arr[] to { 1, 0, 1, 0, 1, 0, 1, 0 }, which is alternating. Therefore, the required output is 2.
Input: arr[] = { 0, 1, 1, 0 }
Output: 1
Explanation:
Reversing the subarray {arr[2], …, arr[2]} modifies arr[] to { 0, 1, 0, 1 }, which is alternating. Therefore, the required output is 1.
Approach: The problem can be solved using Greedy technique. The idea is to count the array elements which are not present at correct indices for the array to be alternating, i.e. count the consecutive equal elements present the given array. Follow the steps below to solve the problem:
- Initialize a variable, say cntOp, to store the minimum count of subarray reversal operations required to make the given array alternating.
- Traverse the array using variable i and for every array element arr[i], check if it is equal to arr[i + 1] or not. If found to be true, then increment the value of cntOp.
- Finally, print the value of (cntOp + 1) / 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumcntOperationReq( int arr[], int N)
{
int cntOp = 0;
for ( int i = 0; i < N - 1; i++) {
if (arr[i] == arr[i + 1]) {
cntOp++;
}
}
return (cntOp + 1) / 2;
}
int main()
{
int arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minimumcntOperationReq(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minimumcntOperationReq( int arr[], int N)
{
int cntOp = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
if (arr[i] == arr[i + 1 ])
{
cntOp++;
}
}
return (cntOp + 1 ) / 2 ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 };
int N = arr.length;
System.out.print(minimumcntOperationReq(arr, N));
}
}
|
Python3
def minimumcntOperationReq(arr, N):
cntOp = 0
for i in range (N - 1 ):
if (arr[i] = = arr[i + 1 ]):
cntOp + = 1
return (cntOp + 1 ) / / 2
arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ]
N = len (arr)
print (minimumcntOperationReq(arr, N))
|
C#
using System;
class GFG
{
static int minimumcntOperationReq( int [] arr, int N)
{
int cntOp = 0;
for ( int i = 0; i < N - 1; i++)
{
if (arr[i] == arr[i + 1])
{
cntOp++;
}
}
return (cntOp + 1) / 2;
}
static void Main()
{
int [] arr = { 1, 1, 1, 0, 1, 0, 0, 0 };
int N = arr.Length;
Console.WriteLine(minimumcntOperationReq(arr, N));
}
}
|
Javascript
<script>
function minimumcntOperationReq(arr, N)
{
let cntOp = 0;
for (let i = 0; i < N - 1; i++)
{
if (arr[i] == arr[i + 1])
{
cntOp++;
}
}
return (cntOp + 1) / 2;
}
let arr = [ 1, 1, 1, 0, 1, 0, 0, 0 ];
let N = arr.length;
document.write(Math.floor(minimumcntOperationReq(arr, N)));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)