Skip to main content

Subsequences with a Unique Middle Mode II - Solution & Explanation

HardPremiumFree on FleetCodeArrayHash TableMathCombinatorics3 min read
Practice this problem

Problem Statement

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] <= 109

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^5)O(1)Small inputs or verifying logic during development
Fix Middle + Frequency CountingO(n^2)O(n)When optimizing brute force while still enumerating left/right choices
Prefix-Suffix CombinatoricsO(n)O(n)General case and expected interview solution for large arrays

Frequently Asked Questions

Is Subsequences with a Unique Middle Mode II easy or hard?
Subsequences with a Unique Middle Mode II is classified as a Hard problem on LeetCode with a low acceptance rate around 14%. The difficulty comes from combining subsequence counting with the unique-mode constraint and deriving an efficient combinatorial counting strategy.
Subsequences with a Unique Middle Mode II Python/Java solution
Most implementations use hash maps or dictionaries to maintain frequency counts while iterating through the array. The same combinatorial logic applies across Python, Java, C++, and Go. The algorithm calculates valid left/right selections around each middle index instead of explicitly generating subsequences.
How to solve Subsequences with a Unique Middle Mode II in O(n)?
Treat each element as the middle of the subsequence and maintain counts of numbers on the left and right using hash maps. Use combinatorial formulas to count pairs selected from both sides while ensuring the middle value appears more times than any other element. Subtract cases where another value ties its frequency. This transforms enumeration into arithmetic counting.
What is the best approach for Subsequences with a Unique Middle Mode II?
The most efficient approach fixes each index as the middle element and counts valid subsequences using prefix and suffix frequency maps combined with combinatorics. Instead of enumerating all subsequences, it calculates how many ways elements can be chosen so the middle value appears most frequently. This reduces the complexity to around O(n) with O(n) additional space.
Is Subsequences with a Unique Middle Mode II asked at Google/Amazon/Meta?
Hard combinatorics and frequency-counting problems like this commonly appear in interviews at companies such as Google, Meta, and Amazon. The question tests array traversal, hash map usage, and combinatorial reasoning under tight constraints.
What data structure is used in Subsequences with a Unique Middle Mode II?
Hash tables are the main data structure used to track prefix and suffix frequencies of elements. Arrays and combinatorial calculations are then used to compute how many valid selections exist around each middle index.
What is the time complexity of Subsequences with a Unique Middle Mode II?
The optimal solution runs in O(n) time by scanning the array once while maintaining prefix and suffix frequency counts. Brute force enumeration requires O(n^5) time because it checks every subsequence of length five. Most competitive implementations rely on combinatorial counting with hash maps to achieve linear performance.

Ready to solve this problem?

Practice Subsequences with a Unique Middle Mode II with our built-in code editor and test cases.

Practice on FleetCode