Ctrl K

Sum of the Tightest Gaps

Hard
You get an integer array values and a number size. Consider every group of exactly size elements of the array (elements at different indices are different even if their values are equal). The tightness of a group is the smallest absolute difference between two of its elements. Return the sum of tightness over all such groups. The answer can be huge, so return it modulo 10⁹ + 7.
Example 1
values=[1, 4, 6], size=2 → 10

There are three pairs: {1, 4} with gap 3, {1, 6} with gap 5 and {4, 6} with gap 2. The sum is 3 + 5 + 2 = 10.

Example 2
values=[5, 1, 3, 9], size=3 → 10

The triples: {1, 3, 5} → 2, {1, 5, 9} → 4, {3, 5, 9} → 2, {1, 3, 9} → 2. The sum is 10.

Example 3
values=[7, 7, 2], size=3 → 0

The only triple has two 7s, so its smallest gap is 0.

Constraints
  • 2 ≤ len(values) ≤ 50
  • -10⁸ ≤ values[i] ≤ 10⁸
  • 2 ≤ size ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardStrict Climb From Spare Parts HardBest-Paying Set of Gigs HardLongest Downhill Route HardBiggest Number Divisible by Three Hard
loading Python…
Input
values=[1, 4, 6], size=2
Expected
10

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