Subsequences with a Unique Middle Mode I - Problem

You're given an integer array nums, and your task is to find the number of subsequences of size 5 where the middle element (3rd position) is the unique mode of that subsequence.

What's a mode? The mode is the element that appears most frequently in a sequence. A unique mode means there's exactly one element that appears more frequently than all others.

What's a unique middle mode? In a 5-element subsequence [a, b, c, d, e], element c (middle) must be the unique mode - it appears more times than any other element in those 5 positions.

Goal: Count all such valid subsequences. Since the answer can be very large, return it modulo 109 + 7.

Example: If nums = [1,2,1,2,1], the subsequence [1,2,1,2,1] has 1 as the middle element, and 1 appears 3 times while 2 appears 2 times, so 1 is the unique mode.

Input & Output

example_1.py โ€” Basic case
$ Input: [1,2,1,2,1]
โ€บ Output: 1
๐Ÿ’ก Note: The subsequence [1,2,1,2,1] has middle element 1 (at index 2), and 1 appears 3 times while 2 appears 2 times, so 1 is the unique mode.
example_2.py โ€” No valid subsequences
$ Input: [1,1,1,1,1]
โ€บ Output: 0
๐Ÿ’ก Note: Any subsequence of size 5 will have all elements equal to 1, so the middle element 1 appears 5 times. Since all positions have the same value, there's no 'unique' mode scenario possible.
example_3.py โ€” Multiple valid subsequences
$ Input: [1,2,3,2,1,2,3]
โ€บ Output: 4
๐Ÿ’ก Note: Multiple subsequences exist where the middle element is the unique mode. For example, [1,2,2,3,1] has 2 as middle with frequency 2, while others have frequency 1.

Constraints

  • 5 โ‰ค nums.length โ‰ค 1000
  • 1 โ‰ค nums[i] โ‰ค 1000
  • The middle element must have strictly higher frequency than any other element in the subsequence

Visualization

Tap to expand
Committee Formation StrategyโญStar Member1234Supporting MembersStep 1: Fix the StarChoose position i as middleStar must appear most frequentlyStep 2: Count AppearancesCount star in left & right partsCalculate total frequencyStep 3: Fill Remaining SpotsNeed 2 before + 2 after middleUse combinatorial formulasStep 4: Verify ConstraintsEnsure star has highest frequencyCount valid combinations
Understanding the Visualization
1
Choose the Star
Pick someone to be the 'star' (middle position with highest frequency)
2
Count Supporters
Count how many times our star appears before and after the middle position
3
Fill Remaining Spots
Calculate valid ways to fill the remaining 4 positions such that our star stays most popular
4
Apply Constraints
Ensure no other person appears as frequently as our star in the final committee
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking all C(n,5) possible subsequences, we fix the middle position and value, then use mathematical formulas to count valid combinations efficiently - reducing time complexity from exponential to polynomial!
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.3K Views
Medium-High Frequency
~35 min Avg. Time
847 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