802. Subsequences with a Unique Middle Mode II

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 10⁹ + 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 the unique mode.

Example 1:

Input: nums = [3,1,3,2,3,4]

Output: 3

Explanation:

There are 6 possible subsequences of size 5. Out of these, three subsequences have the middle element as 3 (the unique mode). For instance, the subsequence [3,1,3,2,3] (formed by indices [0,1,2,3,4]) has 3 appearing three times while 1 and 2 appear only once. In the other valid cases, the frequency of 3 remains strictly higher than that of all other elements.

Example 2:

Input: nums = [1,2,3,3,4,5,3] 

Output: 2

Explanation:

Among the various subsequences of size 5, two examples—such as [1,2,3,4,3] and [1,3,3,5,3]—satisfy the condition. In each case, the middle element is 3 and its frequency in the subsequence is strictly greater than that of any other element, making it the unique mode.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1,2,3,3,4,5,3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 5 <= nums.length <= 10⁵
  • -10⁹ <= nums[i] <= 10⁹

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int subsequencesWithMiddleMode(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums