How to count the subarrays with an equal number of 0s and 1s
Given a fixed-size array containing
To do so, one possible solution is to generate all possible subarrays and count the number of 0s and 1s in each. However, this is a very inefficient solution as it will take
Solution
To simplify this problem of returning the count
equal_subarrays_countof the subarrays with an equal number of 0s and 1s, each time we encounter a 0 in the array while traversing it, we can consider it to be a -1.By doing this, we can now keep track of the sum of all the elements in the array which we have traversed so far in a variable
current_sum, and we also keep track of the number of times each value ofcurrent_sumis encountered.
Each time we encounter a 1, we add 1 to
current_sum, and each time we encounter a 0, we subtract 1 fromcurrent_sum(since we're considering 0 as -1).Now, if we ever encounter a value of
current_sum, which has already been encountered before (it's count is greater than 0), this means that between the indices of the array at which this same value ofcurrent_sumoccurs, the elements visited form a sub-array that has an equal number of 0s and 1s.This is because each element encountered can only increment or decrement
current_sumby 1, and hence, ifcurrent_sumis the same at any 2 indices, it means thatcurrent_sumwas incremented the same number of times it was decremented. This can only happen if the same number of 0s and 1s were encountered between those indices.
Since the count of each value of
current_sumdetermines the number of indices between which the elements sum to the samecurrent_sum, we then add this count ofcurrent_sumtoequal_subarrays_count(since this count determines the number of new subarrays that can be formed containing an equal number of 0s and 1s).We then increment this count by 1 to mark another instance of it occurring.
This process is repeated until we have traversed the whole array, after which we can simply return equal_subarrays_count. The example below demonstrates how this solution works:
Since this solution only requires us to traverse the entire array once, it takes
Example
The code below shows how the algorithm discussed above can be implemented in Python:
def countEqualSubarrays(array , size_of_array):current_sum = 0equal_subarrays_count = 0counts_of_each_currentsum = dict()counts_of_each_currentsum[0] = 1for i in range(0 , size_of_array):if(array[i] == 1):current_sum = current_sum + 1else:current_sum = current_sum - 1if(current_sum in counts_of_each_currentsum):equal_subarrays_count = equal_subarrays_count + counts_of_each_currentsum[current_sum]counts_of_each_currentsum[current_sum] = counts_of_each_currentsum[current_sum] + 1else:counts_of_each_currentsum[current_sum] = 1return equal_subarrays_countarray = [1,0,0,1,0,1,1]print("Array:" , array)print("The number of subarrays with an equal number of 0s and 1s is:" , countEqualSubarrays(array , len(array)))
Explanation
The countEqualSubarrays() function is explained below:
Line 4: We store the number of times each value of
current_sumoccurs in a dictionary, with the key being the value ofcurrent_sumand the value is its count.Lines 9–12: We increment
current_sumif we encounter a 1, or decrement it if we encounter a 0.Lines 14–16: We add the count of
current_sumtoequal_subarrays_countand then increment this count by 1.Lines 17–18: If the value of
current_sumis seen for the first time, we make an entry for it in the dictionary and assign its count the value 1.
Free Resources