Minimum toggles to partition a binary array so that it has first 0s then 1s
Given an array of n integers containing only 0 and 1. Find the minimum toggles (switch from 0 to 1 or vice-versa) required such the array become partitioned, i.e., it has first 0s than 1s. There should be at least one 0 in the beginning, and there can be zero or more 1s in the end.
Input: arr[] = {1, 0, 1, 1, 0}
Output: 2
Toggle the first and last element i.e.,
1 -> 0
0 -> 1
Final array will become:
arr[] = {0 0 1 1 1}
Since first two consecutive elements are all 0s
and rest three consecutive elements are all 1s.
Therefore minimum two toggles are required.
Input: arr[] = {0, 1, 0, 0, 1, 1, 1}
Output: 1
Input: arr[] = {1, 1}
Output: 1
There should be at least one 0.
Input: arr[] = {0, 0}
Output: 0
There can zero 1s.
If we observe the question then we will find that there will definitely exist a point from 0 to n-1 where all elements left to that point should contain all 0’s and right to point should contain all 1’s. Those indices which don’t obey this law will have to be removed. The idea is to count all 0s from left to right.
Let zero[i] denotes the number of 0's till ith
index, then for each i, minimum number of
toggles required can be written as: i - zero[i]
+ zero[n] - zero[i] . The part i - zero[i]
indicates number of 1's to be toggled and the
part zero[n] - zero[i] indicates number of 0's
to be toggled.
After that we just need to take minimum of
all to get the final answer.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minToggle( int arr[], int n)
{
int zero[n + 1];
zero[0] = 0;
for ( int i = 1; i <= n; ++i) {
if (arr[i - 1] == 0)
zero[i] = zero[i - 1] + 1;
else
zero[i] = zero[i - 1];
}
int ans = n;
for ( int i = 1; i <= n; ++i)
ans = min(ans, i - zero[i] + zero[n] - zero[i]);
return ans;
}
int main()
{
int arr[] = { 1, 0, 1, 1, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minToggle(arr, n) << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minToggle( int arr[], int n)
{
int zero[] = new int [n + 1 ];
zero[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; ++i) {
if (arr[i - 1 ] == 0 )
zero[i] = zero[i - 1 ] + 1 ;
else
zero[i] = zero[i - 1 ];
}
int ans = n;
for ( int i = 1 ; i <= n; ++i)
ans = Math.min(ans, i - zero[i] + zero[n]
- zero[i]);
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 1 , 1 , 0 };
int n = arr.length;
System.out.println(minToggle(arr, n));
}
}
|
Python3
def minToggle(arr, n):
zero = [ 0 for i in range (n + 1 + 1 )]
zero[ 0 ] = 0
for i in range ( 1 , n + 1 ):
if (arr[i - 1 ] = = 0 ):
zero[i] = zero[i - 1 ] + 1
else :
zero[i] = zero[i - 1 ]
ans = n
for i in range ( 1 , n + 1 ):
ans = min (ans, i - zero[i] + zero[n] - zero[i])
return ans
arr = [ 1 , 0 , 1 , 1 , 0 ]
n = len (arr)
print (minToggle(arr, n))
|
C#
using System;
class GFG {
static int minToggle( int [] arr, int n)
{
int [] zero = new int [n + 1];
zero[0] = 0;
for ( int i = 1; i <= n; ++i) {
if (arr[i - 1] == 0)
zero[i] = zero[i - 1] + 1;
else
zero[i] = zero[i - 1];
}
int ans = n;
for ( int i = 1; i <= n; ++i)
ans = Math.Min(ans, i - zero[i] +
zero[n] - zero[i]);
return ans;
}
public static void Main()
{
int [] arr = { 1, 0, 1, 1, 0 };
int n = arr.Length;
Console.WriteLine(minToggle(arr, n));
}
}
|
PHP
<?php
function minToggle( $arr , $n )
{
$zero [0] = 0;
$zero [ $n + 1]=0;
for ( $i = 1; $i <= $n ; ++ $i ) {
if ( $arr [ $i - 1] == 0)
$zero [ $i ] = $zero [ $i - 1] + 1;
else
$zero [ $i ] = $zero [ $i - 1];
}
$ans = $n ;
for ( $i = 1; $i <= $n ; ++ $i )
$ans = min( $ans , $i - $zero [ $i ]
+ $zero [ $n ] - $zero [ $i ]);
return $ans ;
}
$arr = array ( 1, 0, 1, 1, 0 );
$n = sizeof( $arr );
echo minToggle( $arr , $n ) , "\n" ;
?>
|
Javascript
<script>
function minToggle(arr, n)
{
let zero = new Array(n + 1);
zero[0] = 0;
for (let i = 1; i <= n; ++i) {
if (arr[i - 1] == 0)
zero[i] = zero[i - 1] + 1;
else
zero[i] = zero[i - 1];
}
let ans = n;
for (let i = 1; i <= n; ++i)
ans = Math.min(ans, i - zero[i] + zero[n] - zero[i]);
return ans;
}
let arr = [ 1, 0, 1, 1, 0 ];
let n = arr.length;
document.write(minToggle(arr, n));
</script>
|
Time complexity: O(n)
Auxiliary space: O(n)
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...