Given a binary array consisting of only zeroes and ones. The task is to find:
- The number of subarrays which has only 1 in it.
- The number of subarrays which has only 0 in it.
Examples:
Input: arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1}
Output:
The number of subarrays consisting of 0 only: 7
The number of subarrays consisting of 1 only: 7Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output:
The number of subarrays consisting of 0 only: 5
The number of subarrays consisting of 1 only: 15
Approach: To count 1’s, the idea is to start traversing the array using a counter. If the current element is 1, increment the counter otherwise add counter*(counter+1)/2 to the number of subarrays and reinitialize counter to 0. Similarly, find the number of subarrays with only 0’s in it.
Below is the implementation of the above approach:
C++
// C++ program to count the number of subarrays // that having only 0's and only 1's #include <bits/stdc++.h> using namespace std; // Function to count number of subarrays void countSubarraysof1and0(int a[], int n) { int count1 = 0, count0 = 0; int number1 = 0, number0 = 0; // Iterate in the array to find count // of subarrays with only 1 in it for (int i = 0; i < n; i++) { // check if array element // is 1 or not if (a[i] == 1) { count1 += 1; } else { number1 += (count1) * (count1 + 1) / 2; count1 = 0; } } // Iterate in the array to find count // of subarrays with only 0 in it for (int i = 0; i < n; i++) { // check if array element // is 0 or not if (a[i] == 0) { count0 += 1; } else { number0 += (count0) * (count0 + 1) / 2; count0 = 0; } } // After iteration completes, // check for the last set of subarrays if (count1) number1 += (count1) * (count1 + 1) / 2; if (count0) number0 += (count0) * (count0 + 1) / 2; cout << "Count of subarrays of 0 only: " << number0; cout << "\nCount of subarrays of 1 only: " << number1; } // Driver Code int main() { int a[] = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 }; int n = sizeof(a) / sizeof(a[0]); countSubarraysof1and0(a, n); return 0; } |
Java
// Java program to count the number of subarrays // that having only 0's and only 1's import java.io.*; class GFG { // Function to count number of subarrays static void countSubarraysof1and0(int a[], int n) { int count1 = 0, count0 = 0; int number1 = 0, number0 = 0; // Iterate in the array to find count // of subarrays with only 1 in it for (int i = 0; i < n; i++) { // check if array element // is 1 or not if (a[i] == 1) { count1 += 1; } else { number1 += (count1) * (count1 + 1) / 2; count1 = 0; } } // Iterate in the array to find count // of subarrays with only 0 in it for (int i = 0; i < n; i++) { // check if array element // is 0 or not if (a[i] == 0) { count0 += 1; } else { number0 += (count0) * (count0 + 1) / 2; count0 = 0; } } // After iteration completes, // check for the last set of subarrays if (count1>0) number1 += (count1) * (count1 + 1) / 2; if (count0>0) number0 += (count0) * (count0 + 1) / 2; System.out.println("Count of subarrays of 0 only: " + number0); System.out.println( "\nCount of subarrays of 1 only: " + number1); } // Driver Code public static void main (String[] args) { int a[] = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 }; int n = a.length; countSubarraysof1and0(a, n);; } } // This code is contributed by inder_verma.. |
Python3
# Python 3 program to count the number of # subarrays that having only 0's and only 1's # Function to count number of subarrays def countSubarraysof1and0(a, n): count1 = 0 count0 = 0 number1 = 0 number0 = 0 # Iterate in the array to find count # of subarrays with only 1 in it for i in range(0, n, 1): # check if array element is 1 or not if (a[i] == 1): count1 += 1 else: number1 += ((count1) * (count1 + 1) / 2) count1 = 0 # Iterate in the array to find count # of subarrays with only 0 in it for i in range(0, n, 1): # check if array element # is 0 or not if (a[i] == 0): count0 += 1 else: number0 += (count0) * (count0 + 1) / 2 count0 = 0 # After iteration completes, # check for the last set of subarrays if (count1): number1 += (count1) * (count1 + 1) / 2 if (count0): number0 += (count0) * (count0 + 1) / 2 print("Count of subarrays of 0 only:", int(number0)) print("Count of subarrays of 1 only:", int(number1)) # Driver Code if __name__ == '__main__': a = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1] n = len(a) countSubarraysof1and0(a, n) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to count the number of subarrays // that having only 0's and only 1's using System; class GFG { // Function to count number of subarrays static void countSubarraysof1and0(int []a, int n) { int count1 = 0, count0 = 0; int number1 = 0, number0 = 0; // Iterate in the array to find count // of subarrays with only 1 in it for (int i = 0; i < n; i++) { // check if array element // is 1 or not if (a[i] == 1) { count1 += 1; } else { number1 += (count1) * (count1 + 1) / 2; count1 = 0; } } // Iterate in the array to find count // of subarrays with only 0 in it for (int i = 0; i < n; i++) { // check if array element // is 0 or not if (a[i] == 0) { count0 += 1; } else { number0 += (count0) * (count0 + 1) / 2; count0 = 0; } } // After iteration completes, // check for the last set of subarrays if (count1>0) number1 += (count1) * (count1 + 1) / 2; if (count0>0) number0 += (count0) * (count0 + 1) / 2; Console.WriteLine("Count of subarrays of 0 only: " + number0); Console.WriteLine( "\nCount of subarrays of 1 only: " + number1); } // Driver Code public static void Main () { int []a = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 }; int n = a.Length; countSubarraysof1and0(a, n);; } } // This code is contributed by inder_verma.. |
PHP
<?php // PHP program to count the number // of subarrays that having only // 0's and only 1's // Function to count number of subarrays function countSubarraysof1and0($a, $n) { $count1 = 0; $count0 = 0; $number1 = 0; $number0 = 0; // Iterate in the array to find count // of subarrays with only 1 in it for ($i = 0; $i <$n; $i++) { // check if array element // is 1 or not if ($a[$i] == 1) { $count1 += 1; } else { $number1 += ($count1) * ($count1 + 1) / 2; $count1 = 0; } } // Iterate in the array to find count // of subarrays with only 0 in it for ($i = 0; $i <$n; $i++) { // check if array element // is 0 or not if ($a[$i] == 0) { $count0 += 1; } else { $number0 += ($count0) * ($count0 + 1) / 2; $count0 = 0; } } // After iteration completes, // check for the last set of subarrays if ($count1) $number1 += ($count1) * ($count1 + 1) / 2; if ($count0) $number0 += ($count0) * ($count0 + 1) / 2; echo "Count of subarrays of 0 only: " , $number0; echo "\nCount of subarrays of 1 only: " , $number1; } // Driver Code $a = array( 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 ); $n = count($a); countSubarraysof1and0($a, $n); // This code is contributed by inder_verma ?> |
Count of subarrays of 0 only: 5 Count of subarrays of 1 only: 15
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Count of subarrays consisting of only prime numbers
- Count of subsequences consisting of the same element
- Count of node sequences of length K consisting of at least one black edge
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Longest subsequence consisting of alternate vowels and consonants
- Construct an AP series consisting of A and B having minimum possible Nth term
- Length of the smallest sub-string consisting of maximum distinct characters
- Length of the longest subsequence consisting of distinct elements
- Make sum of all subarrays of length K equal by only inserting elements
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count of possible subarrays and subsequences using given length of Array
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count subarrays which contains both the maximum and minimum array element
- Queries for decimal values of subarrays of a binary array
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Count subarrays having total distinct elements same as original array
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Count subarrays with non-zero sum in the given Array
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.


