Given an array arr[] consisting of only 0 and 1. The task is to find the minimum number of deletions from the front and back of the array, such that the new modified array consists of an equal number of 0’s and 1’s.
Examples:
Input: arr[] = {1, 1, 0, 1}
Output: 2
Explanation: Two ones from the starting or first and last element can be removed
Input: arr[] = {0, 1, 1, 1, 0}
Output: 3
Explanation: First three elements can be removed or last three elements
Approach: The problem can be solved using greedy approach. As each element of the array can be either 0 or 1, so consider 0 as -1 and 1 as 1 itself. Then find the largest subarray that consists of an equal number of 1s and 0s. Then subtract the size of this subarray from the total size of the array. This approach works because at any moment the approach tries to keep the size of the subarray as large as possible so that only a minimum number of elements from the ends can be removed. See the following diagram for a better understanding.

As it can be seen from the diagram, as the size of middle part y increases which consists of an equal number of 0s and 1s, automatically the size of part (x + z) decreases.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int solve(vector< int > arr)
{
int sz = arr.size();
int summe = 0;
unordered_map< int , int > index;
index[summe] = -1;
int ans = 0, curr_len = 0;
for ( int i = 0; i < sz; i++) {
if (arr[i] == 0)
summe -= 1;
else
summe += 1;
if (index.find(summe) != index.end()) {
curr_len = i - index[summe];
ans = max(ans, curr_len);
}
else
index[summe] = i;
}
return (sz - ans);
}
int main()
{
vector< int > arr = { 1, 1, 0, 1 };
int val = solve(arr);
cout << val;
}
|
Java
import java.util.*;
class GFG{
static int solve( int [] arr)
{
int sz = arr.length;
int summe = 0 ;
HashMap<Integer,Integer> index = new HashMap<Integer,Integer>();
index.put(summe, - 1 );
int ans = 0 , curr_len = 0 ;
for ( int i = 0 ; i < sz; i++) {
if (arr[i] == 0 )
summe -= 1 ;
else
summe += 1 ;
if (index.containsKey(summe) ) {
curr_len = i - index.get(summe);
ans = Math.max(ans, curr_len);
}
else
index.put(summe, i);
}
return (sz - ans);
}
public static void main(String[] args)
{
int []arr = { 1 , 1 , 0 , 1 };
int val = solve(arr);
System.out.print(val);
}
}
|
Python3
from collections import defaultdict
class Solution:
def solve( self , arr):
size = len (arr)
summe = 0
index = defaultdict( int )
index[summe] = - 1
ans = 0
for i in range (size):
if (arr[i] = = 0 ):
summe - = 1
else :
summe + = 1
if (summe in index):
curr_len = i - index[summe]
ans = max (ans, curr_len)
else :
index[summe] = i
return (size - ans)
if __name__ = = "__main__" :
arr = [ 1 , 1 , 0 , 1 ]
obj = Solution()
val = obj.solve(arr)
print (val)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int solve( int [] arr)
{
int sz = arr.Length;
int summe = 0;
Dictionary< int , int > index = new Dictionary< int , int >();
index.Add(summe, -1);
int ans = 0, curr_len = 0;
for ( int i = 0; i < sz; i++) {
if (arr[i] == 0)
summe -= 1;
else
summe += 1;
if (index.ContainsKey(summe) ) {
curr_len = i - index[summe];
ans = Math.Max(ans, curr_len);
}
else
index[summe] = i;
}
return (sz - ans);
}
public static void Main()
{
int []arr = { 1, 1, 0, 1 };
int val = solve(arr);
Console.Write(val);
}
}
|
Javascript
<script>
function solve(arr)
{
size = arr.length
let summe = 0
index = new Map();
index.set(summe, -1);
ans = 0
for (let i = 0; i < size; i++) {
if (arr[i] == 0)
summe -= 1
else
summe += 1
if (index.has(summe)) {
curr_len = i - index.get(summe)
ans = Math.max(ans, curr_len)
}
else
index.set(summe, i)
}
return (size - ans)
}
arr = [1, 1, 0, 1]
val = solve(arr)
document.write(val)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)