Minimum length subarray of 1s in a Binary Array

Given binary array. The task is to find the length of subarray with minimum number of 1s.

Note: It is guaranteed that there is atleast one 1 present in the array.

Examples :



Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}
Output : 3
Minimum length subarray of 1s is {1, 1}.

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

Simple Solution: A simple solution is to consider every subarray and count 1’s in every subarray. Finally return return size of minimum length subarray of 1s.

Efficient Solution: An efficient solution is traverse array from left to right. If we see a 1, we increment count. If we see a 0, and count of 1s so far is positive, calculate minimum of count and result and reset count to zero.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count minimum length
// subarray of 1's in a binary array.
#include <bits/stdc++.h>
using namespace std;
  
// Function to count minimum length subarray
// of 1's in binary array arr[0..n-1]
int getMinLength(bool arr[], int n)
{
    int count = 0; // intitialize count
    int result = INT_MAX; // initialize result
  
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1) {
            count++;
        }
        else {
            if (count != 0)
                result = min(result, count);
            count = 0;
        }
    }
  
    return result;
}
  
// Driver code
int main()
{
    bool arr[] = { 1, 1, 0, 0, 1, 1, 1, 0,
                   1, 1, 1, 1 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << getMinLength(arr, n) << endl;
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count minimum length
// subarray of 1's in a binary array.
import java.io.*;
  
class GFG 
{
      
// Function to count minimum length subarray
// of 1's in binary array arr[0..n-1]
static int getMinLength(double arr[], int n)
{
    int count = 0; // intitialize count
    int result = Integer.MAX_VALUE; // initialize result
  
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] == 1
        {
            count++;
        }
        else 
        {
            if (count != 0)
                result = Math.min(result, count);
            count = 0;
        }
    }
  
    return result;
}
  
// Driver code
public static void main (String[] args) 
{
    double arr[] = { 1, 1, 0, 0, 1, 1, 1, 0,
                1, 1, 1, 1 };
    int n = arr.length;
    System.out.println (getMinLength(arr, n));
  
}
}
  
// This code is contributed by ajit.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count minimum length
# subarray of 1's in a binary array.
import sys
  
# Function to count minimum length subarray
# of 1's in binary array arr[0..n-1]
def getMinLength(arr, n):
    count = 0; # intitialize count
    result = sys.maxsize ; # initialize result
  
    for i in range(n):
        if (arr[i] == 1):
            count+=1;
        else:
            if(count != 0):
                result = min(result, count);
            count = 0;
  
    return result;
  
# Driver code
arr = [ 1, 1, 0, 0, 1, 1, 1, 0,
                1, 1, 1, 1 ];
  
n = len(arr);
  
print(getMinLength(arr, n));
  
# This code is contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count minimum length
// subarray of 1's in a binary array.
using System;
  
class GFG
{
      
// Function to count minimum length subarray
// of 1's in binary array arr[0..n-1]
static int getMinLength(double []arr, int n)
{
    int count = 0; // intitialize count
    int result = int.MaxValue; // initialize result
  
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] == 1) 
        {
            count++;
        }
        else
        {
            if (count != 0)
                result = Math.Min(result, count);
            count = 0;
        }
    }
  
    return result;
}
  
// Driver code
static public void Main ()
{
    double []arr = { 1, 1, 0, 0, 1, 1, 
                     1, 0, 1, 1, 1, 1 };
    int n = arr.Length;
    Console.WriteLine(getMinLength(arr, n));
}
}
  
// This code is contributed by Tushil..
chevron_right

Output:
2

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




Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, Rajput-Ji



Article Tags :
Practice Tags :