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, and it has a unique middle mode of 1. This subsequence can be formed in 6 different ways, so the output is 6.
Example 2:
Input: nums = [1,2,2,3,3,4]
Output: 4
Explanation:
[1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] each have a unique middle mode 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 appear twice.
Example 3:
Input: nums = [0,1,2,3,4,5,6,7,8]
Output: 0
Explanation:
There is no subsequence of length 5 with a unique middle mode.
Constraints:
5 <= nums.length <= 1000-109 <= nums[i] <= 109Problem Overview: You are given an integer array and must count subsequences of length 5 where the third element (the middle) is the unique mode. The middle value must appear more frequently than any other value in that subsequence.
Approach 1: Brute Force Enumeration (O(n^5) time, O(1) space)
The most direct idea is to generate every subsequence of length five using nested loops or combinations. For each candidate subsequence, compute the frequency of all five elements and check whether the third element is the unique mode. This requires counting occurrences and verifying that no other value reaches the same maximum frequency. While simple to reason about, the approach quickly becomes infeasible because the number of subsequences grows as C(n,5). It only works for very small arrays and mainly serves as a correctness baseline.
Approach 2: Prefix/Suffix Frequency Counting + Combinatorics (O(n) time, O(n) space)
The optimized solution fixes each index i as the middle of the subsequence and counts valid ways to pick two elements on the left and two on the right. Use frequency maps to maintain counts of values before and after the current index. The key observation: for the middle value x = nums[i] to be the unique mode, the subsequence must contain additional occurrences of x while ensuring no other number ties its frequency.
Maintain prefix and suffix frequency maps while iterating through the array. For each middle index, compute combinations of positions on the left and right using combinatorics such as C(k,2). Count cases where extra x elements appear on either side and subtract invalid configurations where another value could match the frequency of x. Hash lookups make frequency queries constant time, and combinatorial formulas avoid enumerating subsequences explicitly.
This transforms the problem into counting valid distributions of elements around the middle while tracking frequencies efficiently. The approach relies heavily on hash tables for frequency tracking and combinatorics to compute pair selections. The array is processed once, making it scalable even for large inputs. Understanding how prefix/suffix counts interact with subsequence selection is the main challenge.
Recommended for interviews: Interviewers expect the combinatorics + frequency counting approach. Starting with brute force demonstrates understanding of the constraint (fixed subsequence length), but the optimized solution shows skill with counting techniques and data structures from array processing and hash-based frequency tracking.
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) | Useful only for understanding the definition of a valid subsequence or testing small inputs |
| Prefix/Suffix Frequency + Combinatorics | O(n) | O(n) | General optimal approach for large arrays; counts valid subsequences without explicit enumeration |
3395. Subsequences with a Unique Middle Mode I (Leetcode Hard) • Programming Live with Larry • 206 views views
Practice Subsequences with a Unique Middle Mode I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor