Subarrays with equal 1s and 0s
Given an array arr[] of size N containing 0s and 1s only. The task is to count the subarrays having equal number of 0s and 1s.
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: Testcase 1: 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)
class Solution {
public static int numberOfSubarrays(int[] arr, int n) {
// Turn zeros into -1
for (int i = 0; i < n; i++)
if (arr[i] == 0)
arr[i] = -1;
// Map of sum -> frequency
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int count = 0, sum = 0;
for (int x : arr) {
sum += x;
if (map.containsKey(sum))
count += map.get(sum);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}
}
Last updated