Skip to content
Related Articles

Related Articles

Minimize flips on K-length subarrays required to make all array elements equal to 1
  • Last Updated : 27 Apr, 2021

Given a binary array arr[] of size N and a positive integer K, the task is to find the minimum number of times any subarray of size K from the given array arr[] is required to be flipped to make all array elements equal to 1. If it is not possible to do so, then print “-1”.

Examples:

Input: arr[] = {0, 1, 0}, K = 1
Output: 2
Explanation:
Perform the operation in the following order:
Operation 1: Flip all elements present in the subarray {arr[0]}. Now, the array modifies to {1, 1, 0}.
Operation 2: Flip all elements present in the subarray {arr[2]}. Now the array modifies to {1, 1, 1}.
Therefore, the total number of operations required is 2.

Input: arr[] = {1, 1, 0}, K = 2
Output: -1

 

Approach: Follow the steps below to solve the problem:



  • Initialize an auxiliary array, say isFlipped[] of size N.
  • Initialize a variable, say ans, to store the minimum number of K-length subarray flips required.
  • Traverse the given array arr[] using a variable i and perform the following steps:
    • If the value of i is greater than 0, then update the value of isFlipped[i] as (isFlipped[i] + isFlipped[i – 1])%2.
    • Check if the current element needs to be flipped, i.e. if the value of A[i] is 0 and isFlipped[i] is not set OR, the value of A[i] is 1 and isFlipped[i] is set, then perform the following steps:
      • If such a K-length subarray is not possible, then print “-1” and break out of the loop, as it is impossible to make all array elements equal to 1.
      • Increment ans and isFlipped[i] by 1, and decrement isFlipped[i + K] by 1.
    • Otherwise, continue to the next iteration.
  • After completing the above steps, if all array elements can be made 1, then print the value of ans as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number
// K-length subarrays required to be
// flipped to make all array elements 1
void minimumOperations(vector<int>& A, int K)
{
    // Stores whether an element
    // can be flipped or not
    vector<int> isflipped(A.size(), 0);
 
    // Store the required number of flips
    int ans = 0;
 
    // Traverse the array, A[]
    for (int i = 0; i < A.size(); i++) {
 
        // Find the prefix sum
        // for the indices i > 0
        if (i > 0) {
            isflipped[i] += isflipped[i - 1];
            isflipped[i] %= 2;
        }
 
        // Check if the current element
        // is required to be flipped
        if (A[i] == 0 && !isflipped[i]) {
 
            // If subarray of size K
            // is not possible, then
            // print -1 and return
            if ((A.size() - i + 1) <= K) {
                cout << -1;
                return;
            }
 
            // Increment ans by 1
            ans++;
 
            // Change the current
            // state of the element
            isflipped[i]++;
 
            // Decrement isFlipped[i + K]
            isflipped[i + K]--;
        }
        else if (A[i] == 1 && isflipped[i]) {
 
            // If subarray of size K
            // is not possible, then
            // print -1 and return
            if ((A.size() - i + 1) <= K) {
                cout << -1;
                return;
            }
 
            // Increment ans by 1
            ans++;
 
            // Change the current
            // state of the element
            isflipped[i]++;
 
            // Decrement isFlipped[i+K]
            isflipped[i + K]--;
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 0, 1, 0 };
    int K = 1;
    minimumOperations(arr, K);
 
    return 0;
}

Java




// Java program for the above approach
class GFG
{
 
  // Function to find the minimum number
  // K-length subarrays required to be
  // flipped to make all array elements 1 
  static void minimumOperations(int[] A, int K)
  {
 
    // Stores whether an element
    // can be flipped or not
    int[] isflipped = new int[A.length+1];
 
    // Store the required number of flips
    int ans = 0;
 
    // Traverse the array, A[]
    for (int i = 0; i < A.length; i++)
    {
 
      // Find the prefix sum
      // for the indices i > 0
      if (i > 0) {
        isflipped[i] += isflipped[i - 1];
        isflipped[i] %= 2;
      }
 
      // Check if the current element
      // is required to be flipped
      if (A[i] == 0 && isflipped[i] == 0)
      {
 
        // If subarray of size K
        // is not possible, then
        // print -1 and return
        if ((A.length - i + 1) <= K)
        {
          System.out.println(-1);
          return;
        }
 
        // Increment ans by 1
        ans++;
 
        // Change the current
        // state of the element
        isflipped[i]++;
 
        // Decrement isFlipped[i + K]
        isflipped[i + K]--;
      } else if (A[i] == 1 && isflipped[i] != 0)
      {
 
        // If subarray of size K
        // is not possible, then
        // print -1 and return
        if ((A.length - i + 1) <= K)
        {
          System.out.println(-1);
          return;
        }
 
        // Increment ans by 1
        ans++;
 
        // Change the current
        // state of the element
        isflipped[i]++;
 
        // Decrement isFlipped[i+K]
        isflipped[i + K]--;
      }
    }
 
    // Print the result
    System.out.println(ans);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = {0, 1, 0};
    int K = 1;
    minimumOperations(arr, K);
  }
}
 
// This code is contributed by user_qa7r.

Python3




# Python3 program for the above approach
 
# Function to find the minimum number
# K-length subarrays required to be
# flipped to make all array elements 1
def minimumOperations(A, K):
 
    # Stores whether an element
    # can be flipped or not
    isflipped = [0] * (len(A) + 1)
 
    # Store the required number of flips
    ans = 0
 
    # Traverse the array, A[]
    for i in range(len(A)):
 
        # Find the prefix sum
        # for the indices i > 0
        if (i > 0):
            isflipped[i] += isflipped[i - 1]
            isflipped[i] %= 2
 
        # Check if the current element
        # is required to be flipped
        if (A[i] == 0 and not isflipped[i]):
 
            # If subarray of size K
            # is not possible, then
            # print -1 and return
            if ((len(A) - i + 1) <= K):
                print(-1)
                return
 
            # Increment ans by 1
            ans += 1
 
            # Change the current
            # state of the element
            isflipped[i] += 1
 
            # Decrement isFlipped[i + K]
            isflipped[i + K] -= 1
 
        elif (A[i] == 1 and isflipped[i]):
 
            # If subarray of size K
            # is not possible, then
            # print -1 and return
            if ((len(A) - i + 1) <= K):
                print(-1)
                return
 
            # Increment ans by 1
            ans += 1
 
            # Change the current
            # state of the element
            isflipped[i] += 1
 
            # Decrement isFlipped[i+K]
            isflipped[i + K] -= 1
 
    # Print the result
    print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [0, 1, 0]
    K = 1
     
    minimumOperations(arr, K)
 
# This code is contributed by ukasp

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the minimum number
// K-length subarrays required to be
// flipped to make all array elements 1
static void minimumOperations(List<int> A, int K)
{
     
    // Stores whether an element
    // can be flipped or not
    List<int> isflipped = new List<int>();
    for(int i = 0; i < A.Count + 1; i++)
        isflipped.Add(0);
 
    // Store the required number of flips
    int ans = 0;
 
    // Traverse the array, A[]
    for(int i = 0; i < A.Count; i++)
    {
         
        // Find the prefix sum
        // for the indices i > 0
        if (i > 0)
        {
            isflipped[i] += isflipped[i - 1];
            isflipped[i] %= 2;
        }
 
        // Check if the current element
        // is required to be flipped
        if (A[i] == 0 && isflipped[i] == 0)
        {
             
            // If subarray of size K
            // is not possible, then
            // print -1 and return
            if ((A.Count - i + 1) <= K)
            {
                Console.Write(-1);
                return;
            }
 
            // Increment ans by 1
            ans += 1;
 
            // Change the current
            // state of the element
            isflipped[i] += 1;
 
            // Decrement isFlipped[i + K]
            isflipped[i + K] -= 1;
        }
        else if (A[i] == 1 && isflipped[i] != 0)
        {
             
            // If subarray of size K
            // is not possible, then
            // print -1 and return
            if ((A.Count - i + 1) <= K)
            {
                Console.Write(-1);
                return;
            }
 
            // Increment ans by 1
            ans += 1;
 
            // Change the current
            // state of the element
            isflipped[i] += 1;
 
            // Decrement isFlipped[i+K]
            isflipped[i + K] -= 1;
        }
    }
 
    // Print the result
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main()
{
    List<int> arr = new List<int>(){ 0, 1, 0 };
    int K = 1;
     
    minimumOperations(arr, K);
}
}
 
// This code is contributed by bgangwar59

Javascript




<script>
 
// Javascript program for the above approach
 
  // Function to find the minimum number
  // K-length subarrays required to be
  // flipped to make all array elements 1
  function minimumOperations(A, K)
  {
  
    // Stores whether an element
    // can be flipped or not
    let isflipped = [];
    for (let i = 0; i < A.length + 1; i++)
    {
        isflipped[i] =0;
    }
  
    // Store the required number of flips
    let ans = 0;
  
    // Traverse the array, A[]
    for (let i = 0; i < A.length; i++)
    {
  
      // Find the prefix sum
      // for the indices i > 0
      if (i > 0) {
        isflipped[i] += isflipped[i - 1];
        isflipped[i] %= 2;
      }
  
      // Check if the current element
      // is required to be flipped
      if (A[i] == 0 && isflipped[i] == 0)
      {
  
        // If subarray of size K
        // is not possible, then
        // prlet -1 and return
        if ((A.length - i + 1) <= K)
        {
          document.write(-1);
          return;
        }
  
        // Increment ans by 1
        ans++;
  
        // Change the current
        // state of the element
        isflipped[i]++;
  
        // Decrement isFlipped[i + K]
        isflipped[i + K]--;
      } else if (A[i] == 1 && isflipped[i] != 0)
      {
  
        // If subarray of size K
        // is not possible, then
        // prlet -1 and return
        if ((A.length - i + 1) <= K)
        {
          document.write(-1);
          return;
        }
  
        // Increment ans by 1
        ans++;
  
        // Change the current
        // state of the element
        isflipped[i]++;
  
        // Decrement isFlipped[i+K]
        isflipped[i + K]--;
      }
    }
  
    // Prlet the result
    document.write(ans);
  }
 
 
// Driver Code
 
    let arr = [ 0, 1, 0 ];
    let K = 1;
    minimumOperations(arr, K);
 
</script>
Output: 
2

 

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

 

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 :