Problem Description
Given an integer array nums, count how many subsequences of length 5 have a “unique middle mode”. In a sequence of five elements (selected in their original order) the middle element (index 2 in 0‐indexed order) must be the unique mode – that is, its value must appear more times than any other value in the 5–element subsequence. Since the answer can be huge, return the result modulo 10^9+7.
For example, in nums = [1,2,2,3,3,4] the subsequences [1,2,2,3,4] and [1,2,3,3,4] are valid, whereas [1,2,2,3,3] is not (because both 2 and 3 appear twice).
Key Insights
- The subsequence must consist of 5 indices chosen in increasing order. The element at the third (middle) position is “special.”
- For the middle element’s value (say X) to be the unique mode, X must appear at least twice (in fact, the valid total counts are 2, 3, 4, or 5 in the subsequence); moreover, every other number in the selection must appear at most once.
- It is natural to “fix” the candidate middle index and treat the remaining two indices before and after it independently. In the left block (indices before the middle) and the right block (indices after the middle) we have to choose two positions each.
- For each side, we must count separately the ways to choose indices that contribute a given number of occurrences of the candidate value and, when selecting non–candidate indices, ensure that we pick distinct values (so that no other number can “tie” or exceed the frequency of X).
- By iterating through possible middle indices and “merging” the counts from both sides (with the constraint that the total extra occurrences of X is at least one), we can sum up the valid combinations.
Space and Time Complexity
Time Complexity: O(n · D^2) in the worst case, where n is the length of nums and D is the number of distinct non–candidate values on a side (typically much smaller than n). Since n ≤ 1000 the solution is acceptable. Space Complexity: O(n) for counters and auxiliary storage.
Solution
The main idea is to iterate over every possible index to serve as the middle of a subsequence. For each middle index, consider:
- Let candidate = nums[middle]. The left side consists of indices strictly less than middle and the right side consists of indices greater than middle.
- For each side (left and right) we need to select exactly 2 indices. In doing so we count separately:
- How many ways to choose 0 occurrences of candidate (i.e. pick 2 non–candidate indices with distinct values);
- How many ways to choose 1 occurrence of candidate (i.e. choose 1 candidate index multiplied by a choice of one non–candidate index);
- How many ways to choose 2 occurrences of candidate (only from indices equal to candidate).
- Only those selections where the total extra candidate count (from left and right) is at least 1 are valid (since the chosen middle already contributes one, and a unique mode requires candidate frequency > frequency of any other value).
- For each side, the calculation for non–candidate selections involves enumerating distinct values – if we need one non–candidate index, simply sum counts; if we need two, sum over distinct pairs (v, w) where v ≠ w.
- By multiplying valid left and right selections (summing for appropriate candidate counts on left and right with at least one occurrence in total) and then summing these for every possible middle index, we obtain the answer.
- Modular arithmetic is applied throughout to handle large numbers.
The key “gotcha” is correctly counting ways to choose non–candidate positions without repetition of values because if a non–candidate appears twice then it would tie with candidate frequency when candidate count is 2.