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:
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.. |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Check in binary array the number represented by a subarray is odd or even
- Find minimum length sub-array which has given sub-sequence in it
- Maximum length of subarray such that sum of the subarray is even
- Minimum length of jumps to avoid given array of obstacles
- Minimum number of 1's to be replaced in a binary array
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Minimum number of moves to make a binary array K periodic
- Minimum adjacent swaps required to Sort Binary array
- Maximum sum subarray of even length
- Length of the longest alternating subarray
- Find the length of largest subarray with 0 sum
- Length of the longest Subarray with only Even Elements
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.


