Show problems as
Classic problem #2913EasyArrayHash TableSegment Tree

Subarrays Distinct Element Sum of Squares I

Real-world version: Squared Variety Over Every Window
Tracked in this browser only
Read Subarrays Distinct Element Sum of Squares I on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Readings are given as nums. For a contiguous stretch of them, its variety is how many distinct readings it holds.

Return the total of the squares of the varieties over every non-empty contiguous stretch.

Examples

Example 1

Input
nums = [4, 9, 4, 7, 9]
Output
70

Every one of the fifteen stretches contributes the square of its own variety, and repeats inside a stretch add nothing, which is what keeps this total below what all-distinct readings would give.

Example 2

Input
nums = [3, 8]
Output
6

The two single readings each have variety 1, and the whole pair has variety 2, giving 1 plus 1 plus 4.

Example 3

Input
nums = [3, 3]
Output
3

The pair holds one distinct reading, so all three stretches have variety 1.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def sum_counts(nums: list[int]) -> int:
Java
public int sumCounts(int[] nums)