You are given an integer array nums and two integers a and b.
For a subarray, let:
x be the number of even elements.
y be the number of odd elements.
The ratio of even to odd elements in a subarray is defined as x / y, where ratios are compared by their exact rational values.
A subarray is considered valid if:
y > 0, and
x / y <= a / b.
Return the number of valid subarrays in nums.
Example 1:
Input:nums = [1,2,1,2], a = 3, b = 2
Output:7
Explanation:
The following are the valid subarrays:
Subarray
Values
Even Count
Odd Count
Ratio
nums[0..0]
[1]
0
1
0 / 1
nums[0..1]
[1, 2]
1
1
1 / 1
nums[0..2]
[1, 2, 1]
1
2
1 / 2
nums[0..3]
[1, 2, 1, 2]
2
2
2 / 2
nums[1..2]
[2, 1]
1
1
1 / 1
nums[2..2]
[1]
0
1
0 / 1
nums[2..3]
[1, 2]
1
1
1 / 1
Thus, the number of valid subarrays is 7.
Example 2:
Input:nums = [2,2,1], a = 2, b = 1
Output:3
Explanation:
The following are the valid subarrays:
Subarray
Values
Even Count
Odd Count
Ratio
nums[0..2]
[2, 2, 1]
2
1
2 / 1
nums[1..2]
[2, 1]
1
1
1 / 1
nums[2..2]
[1]
0
1
0 / 1
Thus, the number of valid subarrays is 3.
Example 3:
Input:nums = [2,2,2], a = 1, b = 1
Output:0
Explanation:
Every subarray contains 0 odd numbers, so no subarray is valid.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= a, b <= 109
Solutions
Solution 1: Prefix Sum + Binary Indexed Tree
Thinking
The quadratic enumeration of the previous problem does not survive \(n=10^5\). The condition \(y>0\) and \(\frac{x}{y}\le\frac{a}{b}\) rewrites as \(ay-bx\ge 0\) when \(b>0\); an all-even subarray makes the same expression negative, so both constraints merge.
Mapping odds to \(+a\) and evens to \(-b\), we count nonempty subarrays whose sum is at least \(0\), i.e. prefix pairs with \(s[L]\le s[R]\).
Scanning \(R\), a Fenwick tree on the compressed prefix values stores how many earlier \(s[L]\) have appeared, we query those \(\le s[R]\), then insert the current value.
For a subarray, let \(x\) be the number of even elements and \(y\) be the number of odd elements. The problem requires \(y > 0\) and \(\frac{x}{y} \le \frac{a}{b}\). Since \(b > 0\) and \(y > 0\), the inequality is equivalent to \(a \cdot y - b \cdot x \ge 0\).
When \(y = 0\), since the subarray is non-empty, we must have \(x > 0\). In this case, \(a \cdot y - b \cdot x = -b \cdot x < 0\), so the inequality does not hold. Therefore, the two conditions in the problem can be merged into a single one: \(a \cdot y - b \cdot x \ge 0\).
We treat the odd numbers in \(\textit{nums}\) as \(a\) and the even numbers as \(-b\), resulting in an array \(\textit{arr}\). The original problem is then equivalent to counting the number of non-empty contiguous subarrays of \(\textit{arr}\) whose element sum is at least \(0\).
Let \(s\) be the prefix sum array of \(\textit{arr}\). The element sum of the subarray \([L, R - 1]\) equals \(s[R] - s[L]\), so the problem is further transformed into: how many index pairs \((L, R)\) satisfy \(0 \le L < R \le n\) and \(s[R] - s[L] \ge 0\), i.e., \(s[L] \le s[R]\)?
We enumerate \(R\) and need to quickly count the number of indices \(L\) to the left of \(R\) that satisfy \(s[L] \le s[R]\). This can be maintained with a Binary Indexed Tree: we first discretize all values in \(s\) (sort and deduplicate), then traverse \(s\) from left to right. For each value \(v = s[R]\), we query the number of inserted elements not greater than \(v\) from the Binary Indexed Tree and add it to the answer, then insert \(v\) into the tree.
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{nums}\).