2031. Count Subarrays With More Ones Than Zeros π
Description
You are given a binary array nums containing only the integers 0 and 1. Return the number of subarrays in nums that have more 1's than 0's. Since the answer may be very large, return it modulo 109 + 7.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [0,1,1,0,1] Output: 9 Explanation: The subarrays of size 1 that have more ones than zeros are: [1], [1], [1] The subarrays of size 2 that have more ones than zeros are: [1,1] The subarrays of size 3 that have more ones than zeros are: [0,1,1], [1,1,0], [1,0,1] The subarrays of size 4 that have more ones than zeros are: [1,1,0,1] The subarrays of size 5 that have more ones than zeros are: [0,1,1,0,1]
Example 2:
Input: nums = [0] Output: 0 Explanation: No subarrays have more ones than zeros.
Example 3:
Input: nums = [1] Output: 1 Explanation: The subarrays of size 1 that have more ones than zeros are: [1]
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1
Solutions
Solution 1: Prefix Sum + Binary Indexed Tree
Thinking
Treating \(0\) as \(-1\), a subarray has more ones iff the prefix sum strictly increases. For each right end we count earlier prefixes smaller than the current \(s\). That needs a logarithmic structure.
Prefix sums lie in \([-n,n]\); shifting by \(n+1\) lets a Fenwick tree store frequencies. Insert \(0\) first, query then update, modulo \(10^9+7\).
The problem requires us to count the number of subarrays where the count of \(1\) is greater than the count of \(0\). If we treat \(0\) in the array as \(-1\), then the problem becomes counting the number of subarrays where the sum of elements is greater than \(0\).
To calculate the sum of elements in a subarray, we can use the prefix sum. To count the number of subarrays where the sum of elements is greater than \(0\), we can use a binary indexed tree to maintain the occurrence count of each prefix sum. Initially, the occurrence count of the prefix sum \(0\) is \(1\).
Next, we traverse the array \(nums\), use variable \(s\) to record the current prefix sum, and use variable \(ans\) to record the answer. For each position \(i\), we update the prefix sum \(s\), then query the occurrence count of the prefix sum in the range \([0, s)\) in the binary indexed tree, add it to \(ans\), and then update the occurrence count of \(s\) in the binary indexed tree.
Finally, return \(ans\).
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(n)\). Where \(n\) is the length of the array \(nums\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
Solution 2: Prefix Sum + Ordered Set
Thinking
Solution 1 needs an index shift for the Fenwick tree. A sorted list binary-searches the raw prefix: bisect_left(s) is the count of strictly smaller earlier sums.
The recurrence is the same; an ordered multiset replaces the tree.
Treat \(0\) as \(-1\), keep prefix sums in a sorted list, and binary-search how many earlier sums are smaller.
1 2 3 4 5 6 7 8 9 10 11 | |