Minimum subarray reversals required to make given binary array alternating
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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to count minimum subarray reversal // operations required to make array alternating int minimumcntOperationReq( int arr[], int N) { // Stores minimum count of operations // required to make array alternating int cntOp = 0; // Traverse the array for ( int i = 0; i < N - 1; i++) { // If arr[i] is greater // than arr[i + 1] if (arr[i] == arr[i + 1]) { // Update cntOp cntOp++; } } return (cntOp + 1) / 2; } // Driver Code 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
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to count minimum subarray reversal // operations required to make array alternating static int minimumcntOperationReq( int arr[], int N) { // Stores minimum count of operations // required to make array alternating int cntOp = 0 ; // Traverse the array for ( int i = 0 ; i < N - 1 ; i++) { // If arr[i] is greater // than arr[i + 1] if (arr[i] == arr[i + 1 ]) { // Update cntOp cntOp++; } } return (cntOp + 1 ) / 2 ; } // Driver Code 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)); } } // This code is contributed by code_hunt |
Python3
# Python3 program to implement # the above approach # Function to count minimum subarray # reversal operations required to make # array alternating def minimumcntOperationReq(arr, N): # Stores minimum count of operations # required to make array alternating cntOp = 0 # Traverse the array for i in range (N - 1 ): # If arr[i] is greater # than arr[i + 1] if (arr[i] = = arr[i + 1 ]): # Update cntOp cntOp + = 1 return (cntOp + 1 ) / / 2 # Driver Code arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ] N = len (arr) print (minimumcntOperationReq(arr, N)) # This code is contributed by susmitakundugoaldanga |
C#
// C# program to implement // the above approach using System; class GFG { // Function to count minimum subarray reversal // operations required to make array alternating static int minimumcntOperationReq( int [] arr, int N) { // Stores minimum count of operations // required to make array alternating int cntOp = 0; // Traverse the array for ( int i = 0; i < N - 1; i++) { // If arr[i] is greater // than arr[i + 1] if (arr[i] == arr[i + 1]) { // Update cntOp cntOp++; } } return (cntOp + 1) / 2; } // Driver code static void Main() { int [] arr = { 1, 1, 1, 0, 1, 0, 0, 0 }; int N = arr.Length; Console.WriteLine(minimumcntOperationReq(arr, N)); } } // This code is contributed by divyeshrabadiya07 |
Javascript
<script> // javascript program to implement // the above approach // Function to count minimum subarray reversal // operations required to make array alternating function minimumcntOperationReq(arr, N) { // Stores minimum count of operations // required to make array alternating let cntOp = 0; // Traverse the array for (let i = 0; i < N - 1; i++) { // If arr[i] is greater // than arr[i + 1] if (arr[i] == arr[i + 1]) { // Update cntOp cntOp++; } } return (cntOp + 1) / 2; } // Driver code let arr = [ 1, 1, 1, 0, 1, 0, 0, 0 ]; let N = arr.length; document.write(Math.floor(minimumcntOperationReq(arr, N))); // This code is contributed by splevel62. </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)