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 109 + 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 <= 105-109 <= nums[i] <= 109Problem Overview: You are given an array and must count subsequences of length 5 where the third element is the unique mode of the subsequence. The middle value must appear more times than every other value in that subsequence.
Approach 1: Brute Force Enumeration (O(n^5) time, O(1) space)
Generate every subsequence of length five using five nested loops. For each candidate subsequence, compute the frequency of its elements using a small counter and check whether the third element has strictly higher frequency than all others. The logic is straightforward and useful for verifying correctness on small inputs. The approach becomes impractical once n grows because the number of combinations explodes to O(n^5).
Approach 2: Fix Middle + Frequency Counting (O(n^2) time, O(n) space)
Instead of enumerating all subsequences, fix index i as the middle element. You then choose two elements from the left side and two from the right side. Maintain frequency maps for elements on both sides using a hash table. For each middle position, count combinations that make nums[i] the most frequent value. This reduces the search space dramatically because you only explore combinations around the fixed middle.
Approach 3: Prefix-Suffix Combinatorics (Optimal) (O(n) time, O(n) space)
The optimal solution treats the middle index as the anchor and counts valid combinations using combinatorics. Maintain prefix counts for elements before i and suffix counts for elements after i. For the middle value x, compute how many ways it can appear multiple times in the chosen positions so it becomes the unique mode. Use combinatorial formulas like C(k,2) to count pairs on the left or right, while subtracting cases where another value ties the frequency of x. Hash maps track frequencies dynamically as you iterate across the array. This converts explicit enumeration into arithmetic counting and keeps the runtime linear. The technique combines ideas from array scanning, hash table frequency tracking, and combinatorics.
Recommended for interviews: Start by explaining the brute force idea to show understanding of the constraint (unique mode centered at index 2). Then transition to the prefix–suffix combinatorics approach. Interviewers expect you to reduce enumeration using counting formulas and frequency maps, which demonstrates strong combinatorial reasoning and efficient array traversal.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n^5) | O(1) | Small inputs or verifying logic during development |
| Fix Middle + Frequency Counting | O(n^2) | O(n) | When optimizing brute force while still enumerating left/right choices |
| Prefix-Suffix Combinatorics | O(n) | O(n) | General case and expected interview solution for large arrays |
Practice Subsequences with a Unique Middle Mode II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor