Subsequences with a Unique Middle Mode II - Problem
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.
Definitions:
- A mode of a sequence is the element that appears the maximum number of times in the sequence.
- A sequence has a unique mode if it has only one mode (no ties for most frequent).
- A subsequence of size 5 has a unique middle mode if the middle element (at index 2) is the unique mode of the subsequence.
Note: A subsequence maintains the relative order of elements from the original array but doesn't need to be contiguous.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,1,3,1]
›
Output:
3
💡 Note:
Three valid subsequences with unique middle mode: [1,2,1,3,1] at indices (0,1,2,3,4), (0,1,4,3,2), etc. where middle element has highest frequency uniquely.
Example 2 — No Valid Subsequences
$
Input:
nums = [1,1,1,1,1]
›
Output:
0
💡 Note:
All elements are the same, so no subsequence can have a unique mode - there are always ties.
Example 3 — Minimum Size
$
Input:
nums = [1,2,3,4,5]
›
Output:
0
💡 Note:
All elements are distinct, so in any subsequence of size 5, all elements appear once. No unique mode possible since middle element doesn't have higher frequency.
Constraints
- 5 ≤ nums.length ≤ 2000
- 1 ≤ nums[i] ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given integer array with potentially duplicate elements
2
Find Subsequences
Look for size-5 subsequences where middle element appears most frequently
3
Count Valid
Count subsequences where middle element is unique mode
Key Takeaway
🎯 Key Insight: Fix the middle element and use combinatorics to count valid left/right combinations efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code