The problem statement asks us to find the minimum number of group flips required to convert a given binary array into an array that either contains all 1s or all 0s. A group flip is defined as flipping a continuous sequence of elements in the array.
For example, consider the binary array {1, 1, 0, 0, 0, 1}. We have two choices – we can either make all elements 0 or all elements 1. If we choose to make all elements 0, we need to perform two group flips (from index 2 to 4) to get {1, 1, 0, 0, 0, 0}. If we choose to make all elements 1, we need to perform one group flip (from index 0 to 1) to get {1, 1, 1, 1, 1, 1}. Since making all elements 1 takes the least group flips, we do that.
Similarly, for the input array {1, 0, 0, 0, 1, 0, 0, 1, 0, 1}, we can perform three group flips to get either {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} or {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}. Alternatively, we can perform three separate group flips from index 1 to 3, from index 5 to 6, and from index 8 to 8 to get {1, 1, 1, 0, 1, 0, 0, 1, 1, 1}.
In cases where the input array already consists of all 1s or all 0s, no group flips are needed and the output will be empty. For example, for the input array {0, 0, 0}, the output will be empty because we need not make any change. Similarly, for the input array {1, 1, 1}, the output will be empty.
In cases where the input array consists of only two distinct values, the minimum number of group flips needed will be the same, regardless of whether we choose to make all elements 0 or all elements 1. For example, for the input array {0, 1}, we can either perform one group flip from index 0 to 0 or one group flip from index 1 to 1, and the number of flips required will be the same.
Examples :
Input : arr[] = {1, 1, 0, 0, 0, 1}
Output : From 2 to 4
Explanation : We have two choices, we make all 0s or do all 1s. We need to do two group flips to make all elements 0 and one group flip to make all elements 1. Since making all elements 1 takes least group flips, we do this.
Input : arr[] = {1, 0, 0, 0, 1, 0, 0, 1, 0, 1}
Output :
From 1 to 3
From 5 to 6
From 8 to 8
Input : arr[] = {0, 0, 0}
Output :
Explanation : Output is empty, we need not to make any change
Input : arr[] = {1, 1, 1}
Output :
Explanation : Output is empty, we need not to make any change
Input : arr[] = {0, 1}
Output :
From 0 to 0
OR
From 1 to 1
Explanation : Here number of flips are same either we make all elements as 1 or all elements as 0.
A Naive Solution is to traverse do two traversals of the array. We first traverse to find the number of groups of 0s and the number of groups of 1. We find the minimum of these two. Then we traverse the array and flip the 1s if groups of 1s are less. Otherwise, we flip 0s.
How to do it with one traversal of array?
An Efficient Solution is based on the below facts :
- There are only two types of groups (groups of 0s and groups of 1s)
- Either the counts of both groups are same or the difference between counts is at most 1. For example, in {1, 1, 0, 1, 0, 0} there are two groups of 0s and two groups of 1s. In example, {1, 1, 0, 0, 0, 1, 0, 0, 1, 1}, count of groups of 1 is one more than the counts of 0s.
The given problem is to convert a binary array into an array with either all 0s or all 1s, using the minimum number of group flips. A group flip means flipping a contiguous subarray of the array such that all the elements in that subarray get inverted.
The solution is based on two observations about the given problem. Firstly, there are only two types of groups in the array: groups of 0s and groups of 1s. Secondly, either the counts of both group types are the same or the difference between their counts is at most 1.
Based on these observations, we can conclude that flipping the second group and other groups of the same type as the second group will always lead to the correct answer. If the group counts are the same, it does not matter which group type we flip, as both will lead to the correct answer. If there is one extra group of 1s, we can ignore the first group and start from the second group. By doing this, we convert this case to the first case for the subarray beginning from the second group, and get the correct answer.
For example, let’s consider the binary array {1, 1, 0, 1, 0, 0}. In this array, there are two groups of 0s and two groups of 1s. By flipping the second group (i.e., {0, 0}), we can make all the elements 1. Alternatively, if we flip the first group (i.e., {1, 1}), we can make all the elements 0. Hence, both flipping operations will lead to the correct answer.
Now, let’s consider another example, {1, 1, 0, 0, 0, 1, 0, 0, 1, 1}. In this array, there are two groups of 0s and three groups of 1s. By ignoring the first group of 0s and starting from the second group (i.e., {0, 0}), we can convert this case to the first case for the subarray beginning from the second group. Thus, by flipping the second group and other groups of 0s, we can make all elements 1.
C++
#include <iostream>
using namespace std;
void printGroups( bool arr[], int n) {
for ( int i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) {
if (arr[i] != arr[0])
cout << "From " << i << " to " ;
else
cout << (i - 1) << endl;
}
}
if (arr[n - 1] != arr[0])
cout << (n - 1) << endl;
}
int main() {
bool arr[] = {0, 1, 1, 0, 0, 0, 1, 1};
int n = sizeof (arr) / sizeof (arr[0]);
printGroups(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void printGroups( int arr[], int n)
{
for ( int i = 1 ; i < n; i++)
{
if (arr[i] != arr[i - 1 ])
{
if (arr[i] != arr[ 0 ])
System.out.print( "From " + i + " to " );
else
System.out.println(i - 1 );
}
}
if (arr[n - 1 ] != arr[ 0 ])
System.out.println(n - 1 );
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 };
int n = arr.length;
printGroups(arr, n);
}
}
|
Python3
def printGroups(arr, n):
for i in range ( 1 , n):
if (arr[i] ! = arr[i - 1 ]):
if (arr[i] ! = arr[ 0 ]):
print ( "From" , i, "to " , end = "")
else :
print (i - 1 )
if (arr[n - 1 ] ! = arr[ 0 ]):
print (n - 1 )
if __name__ = = '__main__' :
arr = [ 0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 ]
n = len (arr)
printGroups(arr, n)
|
C#
using System;
class GFG{
static void printGroups( int []arr, int n)
{
for ( int i = 1; i < n; i++)
{
if (arr[i] != arr[i - 1])
{
if (arr[i] != arr[0])
Console.Write( "From " + i + " to " );
else
Console.WriteLine(i - 1);
}
}
if (arr[n - 1] != arr[0])
Console.WriteLine(n - 1);
}
public static void Main(String[] args)
{
int []arr = { 0, 1, 1, 0, 0, 0, 1, 1 };
int n = arr.Length;
printGroups(arr, n);
}
}
|
Javascript
function printGroups(arr, n) {
for (let i = 1; i < n; i++) {
if (arr[i] !== arr[i - 1]) {
if (arr[i] !== arr[0]) {
process.stdout.write(`From ${i} to `);
} else {
console.log(i - 1);
}
}
}
if (arr[n - 1] !== arr[0]) {
console.log(n - 1);
}
}
const arr = [0, 1, 1, 0, 0, 0, 1, 1];
const n = arr.length;
printGroups(arr, n);
|
Output
From 1 to 2
From 6 to 7
Time Complexity: O(n) “The time complexity of this function is O(n), where n is the length of the input array arr. This is because the function loops through the entire array once.”
Auxiliary Space: O(1) “The auxiliary space complexity of this function is O(1), because the amount of extra space used by the function does not depend on the size of the input array. The function only uses a few variables to keep track of indices and values, but the amount of memory used by these variables does not increase as the size of the input array increases.”
Last Updated :
30 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...