Update appNew update is available. Click here to update.
Last Updated: 5 Nov, 2020
Subarray with equal occurrences
Moderate
Problem statement

You have been given an array/list ARR of length N consisting of 0s and 1s only. Your task is to find the number of subarrays(non-empty) in which the number of 0s and 1s are equal.

Input Format:
The first line contains an integer 'T' denoting the number of test cases. Then each test case follows.

The first line of each test case contains a positive integer β€˜N’ which represents the length of the array/list.

The second line of each test case contains β€˜N’ single space-separated integers representing the elements of the array/list.
Output Format:
For each test case, the only line of output will print the number of subarrays in which the number of 0s and 1s are equal. 

Print the output of each test case in a separate line.

Note:

You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 100
1 <= N <= 5 * 10^3
0 <= ARR[i] <= 1

Time limit: 1 sec
Approaches

01Approach

We will visit every subarray using two nested loops and maintain the count of 0s and 1s for each of them. Count of all the subarrays with equal 0s and 1s will be maintained in a β€˜RESULT’ variable.