All problems
0906EasyArrayHash TableSegment Tree

Squared Variety Over Every Window

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2913Subarrays Distinct Element Sum of Squares I

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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