Count subarrays consisting of only 0’s and only 1’s in a binary array
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: 7
Input: 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
Naive Approach-
The idea is to find all subarray and count how many subarrays contain only 1 and how many subarrays contain only 0.
Steps to implement-
- Initialize count0=0 to store the number of subarrays which has only 0 in it.
- Initialize count1=0 to store the number of subarrays which has only 1 in it.
- Run two loops to find all subarrays
- Then check if any subarray has 0 only then increment count0 by 1
- Then check if any subarray has 1 only then increment count1 by 1
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarraysof1and0( int arr[], int N)
{
int count0=0;
int count1=0;
for ( int i=0;i<N;i++){
for ( int j=i;j<N;j++){
bool val1= false ;
bool val2= false ;
int k=i;
while (k<=j){
if (arr[k]!=0){ break ;}
k++;
}
if (k==j+1){val1= true ;}
k=i;
while (k<=j){
if (arr[k]!=1){ break ;}
k++;
}
if (k==j+1){val2= true ;}
if (val1== true ){count0++;}
if (val2== true ){count1++;}
}
}
cout << "Count of subarrays of 0 only: " << count0<<endl;
cout << "Count of subarrays of 1 only: " << count1<<endl;
}
int main()
{
int arr[] = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
countSubarraysof1and0(arr, N);
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
static void countSubarraysof1and0( int [] arr, int N) {
int count0 = 0 ;
int count1 = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i; j < N; j++) {
boolean val1 = false ;
boolean val2 = false ;
int k = i;
while (k <= j) {
if (arr[k] != 0 ) {
break ;
}
k++;
}
if (k == j + 1 ) {
val1 = true ;
}
k = i;
while (k <= j) {
if (arr[k] != 1 ) {
break ;
}
k++;
}
if (k == j + 1 ) {
val2 = true ;
}
if (val1) {
count0++;
}
if (val2) {
count1++;
}
}
}
System.out.println( "Count of subarrays of 0 only: " + count0);
System.out.println( "Count of subarrays of 1 only: " + count1);
}
public static void main(String[] args) {
int [] arr = { 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 };
int N = arr.length;
countSubarraysof1and0(arr, N);
}
}
|
Python
def countSubarraysOf1And0(arr):
N = len (arr)
count0 = 0
count1 = 0
for i in range (N):
for j in range (i, N):
val1 = True
val2 = True
for k in range (i, j + 1 ):
if arr[k] ! = 0 :
val1 = False
break
for k in range (i, j + 1 ):
if arr[k] ! = 1 :
val2 = False
break
if val1:
count0 + = 1
if val2:
count1 + = 1
print ( "Count of subarrays of 0 only:" , count0)
print ( "Count of subarrays of 1 only:" , count1)
if __name__ = = "__main__" :
arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ]
countSubarraysOf1And0(arr)
|
C#
using System;
namespace CountSubarraysOfOnesAndZeros
{
class Program
{
static void CountSubarraysOf1And0( int [] arr, int N)
{
int count0 = 0;
int count1 = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i; j < N; j++)
{
bool val1 = false ;
bool val2 = false ;
int k = i;
while (k <= j)
{
if (arr[k] != 0) { break ; }
k++;
}
if (k == j + 1) { val1 = true ; }
k = i;
while (k <= j)
{
if (arr[k] != 1) { break ; }
k++;
}
if (k == j + 1) { val2 = true ; }
if (val1 == true ) { count0++; }
if (val2 == true ) { count1++; }
}
}
Console.WriteLine($ "Count of subarrays of 0 only: {count0}" );
Console.WriteLine($ "Count of subarrays of 1 only: {count1}" );
}
static void Main( string [] args)
{
int [] arr = { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 };
int N = arr.Length;
CountSubarraysOf1And0(arr, N);
}
}
}
|
Javascript
function countSubarraysOf1And0(arr) {
let count0 = 0;
let count1 = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i; j < arr.length; j++) {
let val1 = false ;
let val2 = false ;
let k = i;
while (k <= j) {
if (arr[k] !== 0) {
break ;
}
k++;
}
if (k === j + 1) {
val1 = true ;
}
k = i;
while (k <= j) {
if (arr[k] !== 1) {
break ;
}
k++;
}
if (k === j + 1) {
val2 = true ;
}
if (val1) {
count0++;
}
if (val2) {
count1++;
}
}
}
console.log( "Count of subarrays of 0 only: " + count0);
console.log( "Count of subarrays of 1 only: " + count1);
}
const arr = [1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1];
countSubarraysOf1And0(arr);
|
Output
Count of subarrays of 0 only: 5
Count of subarrays of 1 only: 15
Time Complexity: O(N3), because of two loops to find all subarrays and the third loop is to find which subarray contains only 1 and which subarray contains only 0
Auxiliary Space: O(1), because no extra space has been used
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++
#include <bits/stdc++.h>
using namespace std;
void countSubarraysof1and0( int a[], int n)
{
int count1 = 0, count0 = 0;
int number1 = 0, number0 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 1) {
count1 ++;
number0 += (count0) * (count0 + 1) / 2;
count0 = 0;
}
else {
count0++;
number1 += (count1) * (count1 + 1) / 2;
count1 = 0;
}
}
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;
}
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
import java.io.*;
class GFG {
static void countSubarraysof1and0( int a[], int n)
{
int count1 = 0 , count0 = 0 ;
int number1 = 0 , number0 = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 1 ) {
count1 += 1 ;
number0 += (count0) * (count0 + 1 ) / 2 ;
count0 = 0 ;
}
else {
count0++;
number1 += (count1) * (count1 + 1 ) / 2 ;
count1 = 0 ;
}
}
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);
}
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);;
}
}
|
Python3
def countSubarraysof1and0(a, n):
count1 = 0
count0 = 0
number1 = 0
number0 = 0
for i in range ( 0 , n, 1 ):
if (a[i] = = 1 ):
number0 + = ((count0) *
(count0 + 1 ) / 2 )
count0 = 0
count1 + = 1
else :
number1 + = ((count1) *
(count1 + 1 ) / 2 )
count1 = 0
count0 + = 1
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))
if __name__ = = '__main__' :
a = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ]
n = len (a)
countSubarraysof1and0(a, n)
|
C#
using System;
class GFG {
static void countSubarraysof1and0( int []a, int n)
{
int count1 = 0, count0 = 0;
int number1 = 0, number0 = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 1) {
count1 += 1;
number0 += (count0) * (count0 + 1) / 2;
count0 = 0;
}
else {
number1 += (count1) * (count1 + 1) / 2;
count1 = 0;
count0++;
}
}
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);
}
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);;
}
}
|
Javascript
<script>
function countSubarraysof1and0(a, n)
{
let count1 = 0, count0 = 0;
let number1 = 0, number0 = 0;
for (let i = 0; i < n; i++) {
if (a[i] == 1) {
count1 += 1;
number0 += (count0) * (count0 + 1) / 2;
count0 = 0;
}
else {
number1 += (count1) * (count1 + 1) / 2;
count1 = 0;
count0 += 1;
}
}
if (count1>0)
number1 += (count1) * (count1 + 1) / 2;
if (count0>0)
number0 += (count0) * (count0 + 1) / 2;
document.write( "Count of subarrays of 0 only: " + number0 + "<br/>" );
document.write( "\nCount of subarrays of 1 only: " + number1);
}
let a = [ 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 ];
let n = a.length;
countSubarraysof1and0(a, n);
</script>
|
PHP
<?php
function countSubarraysof1and0( $a , $n )
{
$count1 = 0; $count0 = 0;
$number1 = 0; $number0 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 1)
{
$count1 += 1;
$number0 += ( $count0 ) *
( $count0 + 1) / 2;
$count0 = 0;
}
else
{
$number1 += ( $count1 ) *
( $count1 + 1) / 2;
$count1 = 0;
$count0 += 1;
}
}
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 ;
}
$a = array ( 1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1 );
$n = count ( $a );
countSubarraysof1and0( $a , $n );
?>
|
Output
Count of subarrays of 0 only: 5
Count of subarrays of 1 only: 15
Complexity Analysis
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Last Updated :
19 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...