Subsequences with a Unique Middle Mode II Problem
Description
LeetCode Problem 3416.
Given an integer array nums, find the number of subsequences of size 5 of nums with a unique middle mode.
Since the answer may be very large, return it modulo 10^9 + 7.
A mode of a sequence of numbers is defined as the element that appears the maximum number of times in the sequence.
A sequence of numbers contains a unique mode if it has only one mode.
A sequence of numbers seq of size 5 contains a unique middle mode if the middle element (seq[2]) is a unique mode.
Example 1:
Input: nums = [1,1,1,1,1,1]
Output: 6
Explanation:
[1, 1, 1, 1, 1] is the only subsequence of size 5 that can be formed from this list, and it has a unique middle mode of 1.
Example 2:
Input: nums = [1,2,2,3,3,4]
Output: 4
Explanation:
[1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] have unique middle modes because the number at index 2 has the greatest frequency in the subsequence. [1, 2, 2, 3, 3] does not have a unique middle mode because 2 and 3 both appear twice in the subsequence.
Example 3:
Input: nums = [0,1,2,3,4,5,6,7,8]
Output: 0
Explanation:
There does not exist a subsequence of length 5 with a unique middle mode.
Constraints:
5 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9
Solution
Approach: Counting With Prefix and Suffix Sums
Fix the middle element at index i with value x. Let a and b be the number of copies of x before and after i, and l and r the number of other values on each side. We pick two elements on each side, and the number of copies of x in the sequence decides whether x is the unique mode:
xappears 3, 4 or 5 times: any other value appears at most twice, so it never ties. Count every combination, e.g. one copy on each side givesa * l * b * r.xappears twice (one more copy on one side): the other three elements must all be different values, otherwise some value appears twice too (or three times).xappears once: the other four elements each appear at least once, soxis never the unique mode.
The only hard case is "three different other values". Count pairs of different values on one side as C(l, 2) minus the pairs with equal values, then remove the choices where the single element on the other side repeats one of the pair's values. Those corrections need sums over all values y of C(L_y, 2), C(R_y, 2), L_y * R_y, L_y^2 * R_y and L_y * R_y^2, where L_y and R_y count y before and after the middle. They change by one value at a time as i moves right, so we update them in O(1).
Complexity: O(n) time and O(n) space.
class Solution:
def subsequencesWithMiddleMode(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
ids = {}
a = [ids.setdefault(x, len(ids)) for x in nums]
m = len(ids)
left = [0] * m # occurrences of each value before the middle
right = [0] * m # occurrences after the middle
for v in a:
right[v] += 1
def c2(x):
return x * (x - 1) // 2
# Sums over all values y of: C(L,2), C(R,2), L*R, L*L*R, L*R*R
sl = sr = p = q = q2 = 0
for v in range(m):
sr += c2(right[v])
ans = 0
for i, x in enumerate(a):
# move nums[i] out of the right side
sr -= c2(right[x])
p -= left[x] * right[x]
q -= left[x] * left[x] * right[x]
q2 -= left[x] * right[x] * right[x]
right[x] -= 1
sr += c2(right[x])
p += left[x] * right[x]
q += left[x] * left[x] * right[x]
q2 += left[x] * right[x] * right[x]
lo = i # elements before / after the middle
ro = n - 1 - i
ax, bx = left[x], right[x] # copies of x on each side
l2, r2 = lo - ax, ro - bx # other values on each side
# x appears 5, 4 or 3 times: the other values can never tie with it
ans += c2(ax) * c2(bx)
ans += c2(ax) * bx * r2 + ax * l2 * c2(bx)
ans += c2(ax) * c2(r2) + c2(l2) * c2(bx) + ax * bx * l2 * r2
# x appears twice: the three other elements must all have different values (it is the only repeat)
pp, qq, qq2 = p - ax * bx, q - ax * ax * bx, q2 - ax * bx * bx
pd_left = c2(l2) - (sl - c2(ax)) # pairs on the left with different values
pd_right = c2(r2) - (sr - c2(bx))
# one x and one other value on the left, two other values on the right (and the mirror image)
d1 = l2 * pd_right - (r2 * pp - qq2)
d2 = r2 * pd_left - (l2 * pp - qq)
ans += ax * d1 + bx * d2
ans %= mod
# move nums[i] to the left side
sl -= c2(left[x])
p -= left[x] * right[x]
q -= left[x] * left[x] * right[x]
q2 -= left[x] * right[x] * right[x]
left[x] += 1
sl += c2(left[x])
p += left[x] * right[x]
q += left[x] * left[x] * right[x]
q2 += left[x] * right[x] * right[x]
return ans