Subsequences with a Unique Middle Mode I Problem
Description
LeetCode Problem 3395.
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, and it has a unique middle mode of 1. This subsequence can be formed in 6 different ways, so the output is 6.
Example 2:
Input: nums = [1,2,2,3,3,4]
Output: 4
Explanation:
[1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] each have a unique middle mode 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 appear twice.
Example 3:
Input: nums = [0,1,2,3,4,5,6,7,8]
Output: 0
Explanation:
There is no subsequence of length 5 with a unique middle mode.
Constraints:
5 <= nums.length <= 1000-10^9 <= nums[i] <= 10^9
Solution
Approach: Enumerate the middle element and count by cases
Fix the middle element at index i with value x, and keep hash maps with the counts of every value to its left and to its right. Choose a copies of x from the left and b from the right, plus 2 - a and 2 - b other values. In the subsequence x then appears 1 + a + b times.
a + b >= 2:xappears at least 3 times, and only 2 other elements remain, soxis always the unique mode. Count every choice with binomials.a + b = 1:xappears twice, so the other 3 elements must all be different. Take all ways to pick them and subtract those with a repeated value (a repeated pair on one side, or a left value equal to a right value), which is a sum over the distinct values.a + b = 0:xappears once and can never be the unique mode.
Sum over all i and take the result modulo 10^9 + 7; every partial count fits in 64 bits, so only the final sum needs the modulo.
Complexity: Time O(n * d) where d is the number of distinct values (at most n), space O(n).
class Solution:
def subsequencesWithMiddleMode(self, nums: List[int]) -> int:
MOD = 10**9 + 7
n = len(nums)
def c2(v: int) -> int:
return v * (v - 1) // 2
left = {}
right = {}
for v in nums:
right[v] = right.get(v, 0) + 1
ans = 0
for i, x in enumerate(nums):
right[x] -= 1
if 2 <= i <= n - 3:
lx, rx = left.get(x, 0), right[x]
A, B = i - lx, n - 1 - i - rx # elements different from x on each side
# x taken at least 3 times in total: nothing else can tie with it
total = 0
for a in range(3):
for b in range(3):
if a + b >= 2:
total += (
self.comb(lx, a) * self.comb(A, 2 - a)
* self.comb(rx, b) * self.comb(B, 2 - b)
)
# x taken twice: the other three elements must be pairwise different
pr = pl = e2 = e2b = 0
for y, cl in left.items():
if y != x:
pl += c2(cl)
cr = right.get(y, 0)
e2 += cl * cr * (B - cr)
e2b += cl * cr * (A - cl)
for y, cr in right.items():
if y != x:
pr += c2(cr)
total += lx * (A * c2(B) - A * pr - e2)
total += rx * (B * c2(A) - B * pl - e2b)
ans += total
left[x] = left.get(x, 0) + 1
return ans % MOD
def comb(self, n: int, k: int) -> int:
if k > n:
return 0
return 1 if k == 0 else (n if k == 1 else n * (n - 1) // 2)