Ctrl K

The Middle as the Sole Favorite (Large Input)

Hard
You are given an integer array values. Consider every way to pick exactly 5 of its elements while keeping their order (subsequences of length 5; picks with different index sets are different even if the numbers match). A pick is good when its middle, third element is the **unique mode**: that value occurs among the five strictly more often than any other value. Count the good picks. The count can be large, so return it modulo 10⁹ + 7. This is the large-input version: you need something much faster than looping over every distinct value for each middle.
Example 1
values=[5, 5, 1, 5, 5, 5] → 3

Six elements give six picks of five, each dropping one element. Dropping index 0, 1 or 2 leaves a 5 in the middle, and 5 is the most frequent value. Dropping index 3, 4 or 5 puts the 1 in the middle, which appears once and is not the mode. That makes 3.

Example 2
values=[1, 2, 3, 2, 1] → 0

The only pick is the whole array. The middle value 3 appears once, while 1 and 2 appear twice each.

Example 3
values=[6, 4, 6, 8, 6] → 1

The middle value 6 appears three times, every other value once.

Constraints
  • 5 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair GCDs by Sorted Position HardThe Middle as the Sole Favorite HardPicks Without a Fixed Gap MediumCorners Made of Ones MediumStars on One Straight Line Hard
loading Python…
Input
values=[5, 5, 1, 5, 5, 5]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.