Skip to content
Related Articles

Related Articles

Minimum Subarray flips required to convert all elements of a Binary Array to K
  • Last Updated : 07 May, 2021

Given a binary array arr[] consisting of N integers, the task is to calculate the minimum number of operations of the following type needed to convert all elements of the array equal to K:

  • Select any index X from the given array.
  • Flip all the elements of the subarray arr[X] … arr[N – 1], i.e., if arr[i] = 1, then set arr[i] as 0 and vice versa.

Examples:

Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 0 
Output:
Explanation: 
Operation 1: X = 0 (chosen index). After modifying values the updated array arr[] is [0, 1, 0, 1, 1, 0, 0, 0]. 
Operation 2: X = 1 (chosen index). After modifying values the updated array arr[] is [0, 0, 1, 0, 0, 1, 1, 1]. 
Operation 3: X = 2 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 1, 1, 0, 0, 0]. 
Operation 4: X = 3 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 1, 1, 1]. 
Operation 5: X = 5 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 0, 0, 0].
Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 1 
Output:4

Approach:The following observations is to be made:

As any index X ( < N) can be chosen and each value from index X to the index N-1 can be modified, so it can be found that the approach is to count the number of changing points in the array.



Follow the steps below to solve the above problem:

  1. Initialize a variable flag to inverse of K. i.e. 1 if K = 0 or vice versa, which denotes the current value.
  2. Initialize a variable cnt to 0, that keeps the count of the number of changing points in the array arr[].
  3. Traverse the array arr[] and for each index i apply the following steps: 
    • If the flag value and arr[i] value are different, go to the next iteration.
    • If both the flag and arr[i] are equal, increase count and set flag to flag = (flag + 1) % 2.
  4. Print the final value of count.

Below is the implementation of the above approach:

C++14




// C++14 Program to implement
// the above appraoch
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the minimum
// number of subarray flips required
int minSteps(int arr[], int n, int k)
{
    int i, cnt = 0;
    int flag;
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for (i = 0; i < n; i++) {
 
        // If arr[i] and flag are equal
        if (arr[i] == flag) {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = sizeof(arr)
            / sizeof(arr[0]);
    int k = 1;
 
    cout << minSteps(arr, n, k);
 
    return 0;
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to count the minimum
// number of subarray flips required
static int minSteps(int arr[], int n, int k)
{
    int i, cnt = 0;
    int flag;
     
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for(i = 0; i < n; i++)
    {
 
        // If arr[i] and flag are equal
        if (arr[i] == flag)
        {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = arr.length;
    int k = 1;
     
    System.out.print(minSteps(arr, n, k));
}
}
 
// This code is contributed by offbeat

Python3




# Python3 program to implement
# the above approach
 
# Function to count the minimum
# number of subarray flips required
def minSteps(arr, n, k):
 
    cnt = 0
    if(k == 1):
        flag = 0
    else:
        flag = 1
 
    # Iterate the array
    for i in range(n):
 
        # If arr[i] and flag are equal
        if(arr[i] == flag):
            cnt += 1
            flag = (flag + 1) % 2
 
    # Return the answer
    return cnt
 
# Driver Code
arr = [ 1, 0, 1, 0, 0, 1, 1, 1 ]
n = len(arr)
k = 1
 
# Function call
print(minSteps(arr, n, k))
 
# This code is contributed by Shivam Singh

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to count the minimum
// number of subarray flips required
static int minSteps(int[] arr, int n, int k)
{
    int i, cnt = 0;
    int flag;
     
    if (k == 1)
        flag = 0;
    else
        flag = 1;
 
    // Iterate the array
    for(i = 0; i < n; i++)
    {
         
        // If arr[i] and flag are equal
        if (arr[i] == flag)
        {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
 
    // Return the answer
    return cnt;
}
 
// Driver code
public static void Main ()
{
    int[] arr = { 1, 0, 1, 0, 0, 1, 1, 1 };
    int n = arr.Length;
    int k = 1;
     
    Console.Write(minSteps(arr, n, k));
}
}
 
// This code is contributed by chitranayal

Javascript




<script>
// JavaScript program for the above approach
 
// Function to count the minimum
// number of subarray flips required
function minSteps(arr, n, k)
{
    let i, cnt = 0;
    let flag;
       
    if (k == 1)
        flag = 0;
    else
        flag = 1;
   
    // Iterate the array
    for(i = 0; i < n; i++)
    {
   
        // If arr[i] and flag are equal
        if (arr[i] == flag)
        {
            cnt++;
            flag = (flag + 1) % 2;
        }
    }
   
    // Return the answer
    return cnt;
}
 
// Driver Code
 
    let arr = [ 1, 0, 1, 0, 0, 1, 1, 1 ];
    let n = arr.length;
    let k = 1;
       
    document.write(minSteps(arr, n, k));
      
</script>
Output: 
4

 

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :