Open In App

Minimum Subarray flips required to convert all elements of a Binary Array to K

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The problem statement is asking for the minimum number of operations required to convert all the elements of a given binary array arr[] to a specified value K, where K can be either 0 or 1. The operations can be performed on any index X of the array, and the operation is to flip all the elements of the subarray arr[X] … arr[N-1].

For example, let’s consider the first input given in the problem statement:

Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 0

Here, the array arr[] has N = 8 elements and the value of K is 0, which means we need to convert all the elements of arr[] to 0. We need to perform a minimum number of operations of the type described above to achieve this.

We can start by selecting an index X of the array, say X = 0, and flip all the elements from arr[0] to arr[N-1]. After this operation, the updated array becomes [0, 1, 0, 1, 1, 0, 0, 0]. We can observe that the first element of the array is 0, which is good. However, the remaining elements are not all equal to 0, so we need to perform another operation.

Next, we can choose the index X = 1 and perform the same operation of flipping all the elements from arr[1] to arr[N-1]. After this operation, the updated array becomes [0, 0, 1, 0, 0, 1, 1, 1]. We can again observe that the first two elements of the array are now 0, but the remaining elements are still not all equal to 0, so we need to perform more operations.

We can continue this process by selecting different indices and performing the flip operation until all the elements of the array become 0. In this case, we need to perform a total of 5 operations to achieve this goal.

Similarly, for the second input given in the problem statement:

Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 1

Here, the value of K is 1, which means we need to convert all the elements of arr[] to 1. We can follow the same process as described above, but we need to flip the elements of the subarray only when they are not already equal to 1. After performing a minimum number of operations, we can convert all the elements of arr[] to 1.

In this case, we need to perform a total of 4 operations to achieve this goal.

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 approach
#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: The function minSteps iterates through the given array once, so the time complexity is O(n), where n is the size of the array.

Auxiliary Space: The function minSteps uses a constant amount of extra space, regardless of the size of the input array. So, the auxiliary space complexity is O(1).

Space Complexity: In the given program, the input array is stored in memory, which requires O(n) space. The function minSteps uses O(1) auxiliary space. So, the total space complexity of the program is O(n) as the input array size dominates the space complexity.
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads