Subsequences with a Unique Middle Mode II - Problem

Given an integer array nums, find the number of subsequences of size 5 where the middle element (3rd element) is the unique mode of the subsequence.

A mode is the element that appears most frequently in a sequence. A sequence has a unique mode if exactly one element appears with the highest frequency.

For a subsequence of size 5: [a, b, c, d, e], element c (the middle element) must be the unique mode. This means c appears more frequently than any other element in the subsequence.

Example: In subsequence [1, 2, 3, 3, 4], element 3 appears twice while others appear once, so 3 is the unique mode.

Since the answer can be very large, return it modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: [1, 2, 3, 3, 4]
› Output: 3
šŸ’” Note: Valid subsequences: [1,2,3,3,4] (middle=3, appears 2 times, others appear 1 time), and two other arrangements where 3 is the unique mode in middle position.
example_2.py — No Valid Subsequences
$ Input: [1, 2, 3, 4, 5]
› Output: 0
šŸ’” Note: All elements are distinct, so no element can be a mode (appear more than once). No valid subsequences exist.
example_3.py — Multiple Same Elements
$ Input: [1, 1, 1, 1, 1]
› Output: 1
šŸ’” Note: Only one subsequence [1,1,1,1,1] where middle element 1 is the unique mode (appears 5 times, no other elements exist).

Constraints

  • 5 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000
  • Return answer modulo 109 + 7

Visualization

Tap to expand
Committee Formation: Ensuring Chair's Party DominanceRRepublicanDDemocratGGreenAvailable PoliticiansStep 1: Choose Chair Position (Middle of Committee)RRRCHAIRDGLeft: 2 RepublicansChairRight: 1D, 1GStep 2: Calculate Valid Committee FormationsCombinatorial Analysis:• Ways to pick additional Republicans: C(2,1) = 2• Ways to pick from other parties: C(2,1) = 2• Total committees where Republicans dominate: 2 Ɨ 2 = 4āœ“ Success Condition:Republicans: 3 membersOther parties: ≤ 1 eachClear majority achieved!šŸŽÆ Key Insight: Fix the chair first, then count mathematically instead of trying all combinations!
Understanding the Visualization
1
Choose Committee Chair
The middle person becomes the committee chair - their party must have the most members
2
Count Party Members
Count how many members of chair's party are available on left and right sides
3
Calculate Combinations
Use math to count ways to form committee without chair's party being tied with others
4
Verify Majority
Ensure chair's party has strictly more members than any other party in the committee
Key Takeaway
šŸŽÆ Key Insight: By fixing the middle element and using combinatorial formulas, we avoid the exponential explosion of generating all possible subsequences, reducing complexity from O(n⁵) to O(n²).
Asked in
Google 25 Meta 18 Microsoft 15 Amazon 12
27.2K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen