Skip to main content

Subsequences with a Unique Middle Mode I - Solution & Explanation

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

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^5)O(1)Useful only for understanding the definition of a valid subsequence or testing small inputs
Prefix/Suffix Frequency + CombinatoricsO(n)O(n)General optimal approach for large arrays; counts valid subsequences without explicit enumeration

Video Solution

3395. Subsequences with a Unique Middle Mode I (Leetcode Hard)Programming Live with Larry209 views views

Frequently Asked Questions

Is Subsequences with a Unique Middle Mode I easy or hard?
Subsequences with a Unique Middle Mode I is classified as Hard. The difficulty comes from translating the "unique mode" requirement into combinatorial counting rules and efficiently maintaining frequency information while iterating through the array.
Subsequences with a Unique Middle Mode I Python/Java solution
Implement the optimized approach using hash maps or dictionaries to track prefix and suffix counts. For each index as the middle element, compute valid combinations using combinatorics and update the answer. The same logic works across Python, Java, C++, and Go with language‑specific map implementations.
How to solve Subsequences with a Unique Middle Mode I in O(n)?
Iterate through the array and treat each element as the potential middle of a length‑5 subsequence. Maintain hash maps that track frequencies of values to the left and right. Use combinatorial formulas such as C(k,2) to count ways to pick pairs while ensuring the middle value becomes the unique mode.
What is the best approach for Subsequences with a Unique Middle Mode I?
The most efficient approach fixes each index as the middle element and counts valid subsequences using prefix and suffix frequency maps. Combinatorics is used to calculate how many ways two elements can be chosen from each side. This avoids enumerating subsequences and reduces the complexity to about O(n) time with O(n) space.
Is Subsequences with a Unique Middle Mode I asked at Google/Amazon/Meta?
Problems involving frequency counting, combinatorics, and subsequence counting appear frequently in interviews at companies like Google, Amazon, and Meta. While this exact problem may not appear verbatim, the techniques used here—hash maps and combinatorial counting—are common interview patterns.
What data structure is used in Subsequences with a Unique Middle Mode I?
Hash tables (hash maps) are the primary data structure. They store frequency counts of elements on the left and right of the current middle index, enabling constant‑time updates and lookups during the iteration.
What is the time complexity of Subsequences with a Unique Middle Mode I?
The optimal solution runs in O(n) time because the array is scanned once while maintaining prefix and suffix frequency counts. Each step performs constant-time hash map lookups and combinatorial calculations. Space complexity is O(n) for storing frequency information.

Ready to solve this problem?

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

Practice on FleetCode