Subarrays Distinct Element Sum of Squares I

Easy Topics Company Tags

You are given an integer array nums.

The distinct count of a subarray nums[i..j] (all the elements from index i to index j, where 0 <= i <= j < nums.length) is the number of distinct values in it.

Return the sum of the squares of the distinct counts of all subarrays of nums.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,2,1]

Output: 15

Explanation: The six subarrays are:

  • [1], [2] and [1]: 1 distinct value each.
  • [1,2], [2,1] and [1,2,1]: 2 distinct values each.

The sum is 1² + 1² + 1² + 2² + 2² + 2² = 15.

Example 2:

Input: nums = [1,1]

Output: 3

Explanation: The three subarrays [1], [1] and [1,1] each have 1 distinct value, so the sum is 1² + 1² + 1² = 3.

Constraints:

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


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.