Count subarrays having an equal count of 0s and 1s segregated
Last Updated :
21 Nov, 2021
Given a binary array arr[], the task is to count the number of subarrays having equal count of 0s and 1s, and all the 0s and 1s are placed consecutively in that subarray.
Examples:
Input: arr[] = {1, 0, 1, 1}
Output: 2
Explanation: The subarrays satisfying the given conditions are {1, 0} and {0, 1}. Therefore, the count of such subarrays is 2.
Input: arr[] = {1, 1, 0, 0, 1, 0}
Output: 4
Explanation: The subarrays satisfying the given conditions are {1, 1, 0, 0}, {1, 0}, {0, 1}, {1, 0}. Therefore, the count of such subarrays is 4.
Naive Approach: The simplest approach is to traverse the given array and for every pair of unequal adjacent elements, iterate the left and right of the current index and check if the count of 1s and 0s are equal or not. Increment the count of subarrays until found to be false. After complete traversal of the array, print the total count of subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarrays( int A[], int N)
{
int ans = 0;
for ( int i = 0; i < N - 1; i++) {
if (A[i] != A[i + 1]) {
ans++;
for ( int j = i - 1, k = i + 2;
j >= 0 && k < N
&& A[j] == A[i]
&& A[k] == A[i + 1];
j--, k++) {
ans++;
}
}
}
cout << ans << "\n" ;
}
int main()
{
int A[] = { 1, 1, 0, 0, 1, 0 };
int N = sizeof (A) / sizeof (A[0]);
countSubarrays(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void countSubarrays( int A[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
if (A[i] != A[i + 1 ])
{
ans++;
for ( int j = i - 1 , k = i + 2 ;
j >= 0 && k < N
&& A[j] == A[i]
&& A[k] == A[i + 1 ];
j--, k++)
{
ans++;
}
}
}
System.out.print(ans+ "\n" );
}
public static void main(String[] args)
{
int A[] = { 1 , 1 , 0 , 0 , 1 , 0 };
int N = A.length;
countSubarrays(A, N);
}
}
|
Python3
def countSubarrays(A, N) :
ans = 0 ;
for i in range (N - 1 ) :
if (A[i] ! = A[i + 1 ]) :
ans + = 1 ;
j = i - 1 ; k = i + 2 ;
while (j > = 0 and k < N and A[j] = = A[i] and A[k] = = A[i + 1 ]) :
ans + = 1 ;
j - = 1 ;
k + = 1 ;
print (ans);
if __name__ = = "__main__" :
A = [ 1 , 1 , 0 , 0 , 1 , 0 ];
N = len (A);
countSubarrays(A, N);
|
C#
using System;
class GFG{
static void countSubarrays( int [] A, int N)
{
int ans = 0;
for ( int i = 0; i < N - 1; i++)
{
if (A[i] != A[i + 1])
{
ans++;
for ( int j = i - 1, k = i + 2;
j >= 0 && k < N &&
A[j] == A[i] &&
A[k] == A[i + 1];
j--, k++)
{
ans++;
}
}
}
Console.Write(ans + "\n" );
}
public static void Main()
{
int [] A = { 1, 1, 0, 0, 1, 0 };
int N = A.Length;
countSubarrays(A, N);
}
}
|
Javascript
<script>
function countSubarrays(A, N)
{
let ans = 0;
for (let i = 0; i < N - 1; i++)
{
if (A[i] != A[i + 1])
{
ans++;
for (let j = i - 1, k = i + 2;
j >= 0 && k < N
&& A[j] == A[i]
&& A[k] == A[i + 1];
j--, k++)
{
ans++;
}
}
}
document.write(ans+ "<br/>" );
}
let A = [ 1, 1, 0, 0, 1, 0 ];
let N = A.length;
countSubarrays(A, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, follow the steps below:
- Initialize a variable, say res, to store the count of subarrays.
- Initialize a variable, say curr, with the first value of the array and an array cnt[], to keep track of the consecutive elements.
- Traverse the array and perform the following steps:
- If the current element is equal to curr, increment the last value of cnt[].
- Otherwise, update curr to the current element and append 1 to the array cnt[].
- Traverse the array cnt[] and find the sum of the minimum of the adjacent elements and add it to the variable res. This ensures that the frequency of the elements is equal.
- After completing the above steps, print the value of res as the resultant count of subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarrays( int A[], int N)
{
int res = 0;
int curr = A[0];
vector< int > cnt = {1};
for ( int c = 1; c < N; c++)
{
if (A == curr)
cnt[cnt.size() - 1]++;
else
curr = A;
cnt.push_back(1);
}
for ( int i = 1; i < cnt.size(); i++)
{
res += min(cnt[i - 1], cnt[i]);
}
cout << (res - 1);
}
int main()
{
int A[] = { 1, 1, 0, 0, 1, 0 };
int N = sizeof (A) / sizeof (A[0]);
countSubarrays(A, N);
return 0;
}
|
Java
import java.util.Vector;
class GFG {
static void countSubarrays( int [] A)
{
int res = 0 ;
int curr = A[ 0 ];
int [] cnt = new int [A.length];
cnt[ 0 ] = 1 ;
for ( int c = 1 ; c < A.length; c++) {
if (A == curr)
cnt++;
else
curr = A;
cnt = 1 ;
}
for ( int i = 1 ; i < cnt.length; i++) {
res += Math.min(cnt[i - 1 ], cnt[i]);
}
System.out.println(res - 1 );
}
public static void main(String[] args)
{
int [] A = { 1 , 1 , 0 , 0 , 1 , 0 };
countSubarrays(A);
}
}
|
Python3
def countSubarrays(A):
res = 0
curr, cnt = A[ 0 ], [ 1 ]
for c in A[ 1 :]:
if c = = curr:
cnt[ - 1 ] + = 1
else :
curr = c
cnt.append( 1 )
for i in range ( 1 , len (cnt)):
res + = min (cnt[i - 1 ], cnt[i])
print (res - 1 )
A = [ 1 , 1 , 0 , 0 , 1 , 0 ]
countSubarrays(A)
|
C#
using System;
class GFG{
static void countSubarrays( int [] A)
{
int res = 0;
int curr = A[0];
int [] cnt = new int [A.Length];
cnt[0] = 1;
for ( int c = 1; c < A.Length; c++)
{
if (A == curr)
cnt++;
else
curr = A;
cnt = 1;
}
for ( int i = 1; i < cnt.Length; i++)
{
res += Math.Min(cnt[i - 1], cnt[i]);
}
Console.WriteLine(res - 1);
}
public static void Main(String[] args)
{
int [] A = { 1, 1, 0, 0, 1, 0 };
countSubarrays(A);
}
}
|
Javascript
<script>
function countSubarrays( A, N)
{
var res = 0;
var curr = A[0];
var cnt = [];
cnt.fill(1)
for ( var c = 1; c < N; c++)
{
if (A == curr)
cnt[cnt.length - 1]++;
else
curr = A;
cnt.push(1);
}
for ( var i = 1; i < cnt.length; i++)
{
res += Math.min(cnt[i - 1], cnt[i]);
}
document.write (res);
}
var A = [ 1, 1, 0, 0, 1, 0 ];
var N = A.length;
countSubarrays(A, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...