Problem Description
Given an integer array nums, count how many subsequences (with indices in increasing order) of length 5 have a “unique middle mode” – that is, when the 3rd element (index 2 in the subsequence) is viewed together with the other four chosen numbers, it is the only value that attains the maximum frequency within that subsequence. Because there can be many answers, return the result modulo 10^9 + 7.
Key Insights
- One may “fix” the candidate for the middle position. Since the subsequence must contain 5 numbers, the middle candidate must come from an index having at least two indices to its left and at least two to its right.
- With a fixed middle element (say with value x at index i), the subsequence is [a, b, x, c, d] where the two positions a,b come from the left part and c,d from the right.
- The unique mode condition means that after adding the fixed middle occurrence (value x), even if additional copies of x are included among a,b,c,d, no other number may appear with a frequency as high as x’s. (In other words, if the extra copies of x total r (possibly 0,1,2,3,4) so that x appears r+1 times, then every other number must appear at most r times.)
- The structure of choosing 2 indices from the left and 2 from the right suggests a “divide‐and‐conquer” by pre‐computing combinatorial counts on each side.
- For each half one must “bucket” the pair selections by two pieces of information: how many times the candidate x is chosen and, for the non‑x numbers, whether (and if so, how often) the same number is chosen so that one could later “merge” and check the unique mode condition.
Space and Time Complexity
Time Complexity: O(n * (combinatorial factors from left/right processing))
In a careful solution the per‑candidate work is roughly constant (after preprocessing) so overall O(n).
Space Complexity: O(n + M) where M is the size needed for counting frequencies in prefix/suffix (or using hash maps keyed by the number values).
Solution
The solution fixes every possible candidate for the middle position (ensuring at least two numbers on either side). For each candidate x at index i:
- We must choose two indices from the left [0, i‑1] and two from the right [i+1, n‑1]. Let these choices be denoted L and R.
- In L and R, the candidate x could be chosen additionally. Denote rL and rR as the number of extra copies of x selected (0, 1, or 2 on each side) so that overall x appears r = rL + rR extra times and total frequency = r+1.
- The remaining numbers in L and R (that are not equal to x) will be “others.” When some other value y appears among the four slots, we must have count(y) ≤ r so that x (with frequency r+1) is the unique mode.
- To make counting efficient, we precompute for each prefix and suffix the “pair‐statistics”: that is, for any candidate position, we know in the left how many ways there are to pick a pair that: – Controls for how many copies of x are used; – And whether the two “other” numbers come from the same value (which could “spoil” the mode condition when r is small) or from distinct values.
- Once we count valid pairs for left and right given a particular split (say, f_left copies on left and f_right copies on right so that f_left+f_right=r), we merge counts by “convolving” over the possibilities from the left and right provided that when merged the maximum frequency among any “other” number is ≤ r.
- Finally, add over all candidates and take the answer modulo 10^9 + 7.
The challenge in the solution is in “bucketing” the pair selections on the left (and similarly on the right) into types. There are three types for choosing a pair from one side: • Both numbers equal to x. • One number equal to x and one number not equal to x. • Both numbers not equal to x. (And in this case, one must distinguish whether the two chosen numbers are equal (frequency 2 for that non‑x) or distinct (each with frequency 1).) Then, when merging left and right counts, the aggregated non‑x frequencies (if the same number appears in both halves) must be tracked so that none exceeds the extra x count.
A careful implementation uses hash tables and precomputed “combination” counts (often with a two‐pointer or prefix‑sums strategy) so that each candidate can be processed in O(1)–O(1) amortized time after O(n) preprocessing.
An important trick is to “fix the pivot” (middle element candidate) and count valid quadruples from the left and right side separately; then, when merging, use the observation that valid cases for a given additional x count r require that any non‑x element appears at most r times overall. In particular, if r is 0 then the non‑x pair from both sides must not “collide” (i.e. they must be distinct), a constraint that must be enforced while counting.
Code Solutions
Below are code solutions in Python, JavaScript, C++ and Java. (These snippets include line‐by‐line comments and use placeholders as required.)