Skip to content
Related Articles

Related Articles

Minimum subarray reversals required to make given binary array alternating
  • Last Updated : 04 Dec, 2020

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:
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:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

2

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :