Ctrl K

Squared Variety Total

Hard
You are given an integer array values. The variety of a non-empty window (a run of consecutive elements) is the number of distinct values it contains. Square the variety of every window of the array and add the results up. The total can be huge, so return it modulo 10⁹ + 7.
Example 1
values=[3, 3, 5] → 12

The windows [3], [3], [5] and [3, 3] hold one distinct value each, while [3, 5] and [3, 3, 5] hold two: 1 + 1 + 1 + 1 + 4 + 4 = 12.

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

Three windows of length 1 give 1 each, two of length 2 give 4 each, and the whole array gives 9: 20 in total.

Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRising Chain With Small Steps HardSteep Subsequence With the Largest Sum HardCounting the Longest Rising Selections MediumSteady Climb or Drop Trios MediumCity Block Outline Hard
loading Python…
Input
values=[3, 3, 5]
Expected
12

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