Imagine you're analyzing a series of binary signals where 1 represents success and 0 represents failure. Your task is to find all possible contiguous segments where successes outnumber failures.
Given a binary array nums containing only 0s and 1s, return the number of subarrays that have more 1's than 0's. Since the answer could be astronomically large, return it modulo 109 + 7.
What's a subarray? A subarray is any contiguous sequence of elements within the array. For example, in [1,0,1], the subarrays are: [1], [0], [1], [1,0], [0,1], and [1,0,1].
Input & Output
Visualization
Time & Space Complexity
O(n) for traversal, but we need O(log n) for each lookup to count smaller elements, achieved using ordered data structures
Hash map to store all prefix sum frequencies
Constraints
- 1 โค nums.length โค 105
- nums[i] is either 0 or 1
- Return answer modulo 109 + 7