Ctrl K

Extremes of Small Groups

Medium
You are given an array values and an integer limit. Consider every way to choose a group of 1 to limit elements from the array (elements are told apart by position, even if their values are equal). For each group, add its minimum to its maximum. Return the total of these numbers over all groups. Since it can be huge, return it modulo 10⁹ + 7.
Example 1
values=[4, 1, 3], limit=2 → 32

Single-element groups give 8 + 2 + 6 = 16. Pairs: {4, 1} → 5, {4, 3} → 7, {1, 3} → 4, another 16. The answer is 32.

Example 2
values=[2, 2], limit=1 → 8

Only single-element groups: each has minimum and maximum 2, so 4 + 4 = 8.

Example 3
values=[0, 5, 10], limit=3 → 70
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁹
  • 1 ≤ limit ≤ min(70, len(values))
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPicks Without a Fixed Gap MediumSets Without a Fixed Gap MediumChain of Multiples MediumSubsets With an Odd Total MediumBiggest Number Divisible by Three Hard
loading Python…
Input
values=[4, 1, 3], limit=2
Expected
32

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