Count subarrays with equal number of 1’s and 0’s
Submissions: 1710   Accuracy:

31.45%

  Difficulty: Medium   Marks: 4
Associated Course(s):   Sudo Placement [IITs]
       

Given an array of size N containing 0 and 1 only. The task is to count the subarrays having equal number of 0’s and 1’s.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains an Integer N denoting size of array and the second line contains N space separated 0 and 1.

Output:
For each test case, print the count of required sub arrays in new line.

Constraints:
1<=T<=100
1<=N<=106

0<=A[i]<=1

Example:
Input:

2
7
1 0 0 1 0 1 1
5
1 1 1 1 0

Output:
8
1

Explanation:

Input : A[] = {1, 0, 0, 1, 0, 1, 1}
Output : 8
The index range for the 8 sub-arrays are:
(0, 1), (2, 3), (0, 3), (3, 4), (4, 5)
(2, 5), (0, 5), (1, 6)

** For More Input/Output Examples Use 'Expected Output' option **

Author: arun03


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.