Skip to content
Related Articles

Related Articles

Minimum length subarray of 1s in a Binary Array
  • Last Updated : 06 Jun, 2019

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:

C++




// 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;
}


Java




// 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.


Python3




# 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


C#




// 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..


Output:

2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :