The score of an index i is defined as the number of indices j such that:
i < j < n
nums[j] < nums[i]
nums[i] and nums[j] have different parity (one is even and the other is odd).
Return an integer array answer of length n, where answer[i] is the score of index i.
Β
Example 1:
Input:nums = [5,2,4,1,3]
Output:[2,1,2,0,0]
Explanation:
For i = 0, the elements nums[1] = 2 and nums[2] = 4 are smaller and have different parity.
For i = 1, the element nums[3] = 1 is smaller and has different parity.
For i = 2, the elements nums[3] = 1 and nums[4] = 3 are smaller and have different parity.
No valid elements exist for the remaining indices.
Thus, the answer = [2, 1, 2, 0, 0].
Example 2:
Input:nums = [4,4,1]
Output:[1,1,0]
Explanation:βββββββ
For i = 0 and i = 1, the element nums[2] = 1 is smaller and has different parity. Thus, the answer = [1, 1, 0].
Example 3:
Input:nums = [7]
Output:[0]
Explanation:
No elements exist to the right of index 0, so its score is 0. Thus, the answer = [0].
Β
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109βββββββ
Solutions
Solution 1: Ordered List or Binary Indexed Tree
We can use two ordered lists (or Binary Indexed Trees) to separately maintain even and odd elements. For each element, we query the number of smaller elements in the other list, and then add the current element to its corresponding list.
The time complexity is \(O(n \times \log n)\) and the space complexity is \(O(n)\), where \(n\) is the length of the array.