Count the number of subarrays that contain every distinct value present in the whole array.
Given an integer array nums, a subarray is called complete if it contains all distinct values that appear anywhere in nums.
Return the number of complete subarrays.
A subarray is a contiguous non-empty segment of the array.
nums.1 <= nums.lengthnums contains integers.nums: an integer arrayFind the number of contiguous subarrays that contain every distinct value appearing in nums.
Example 1
Input
nums = [1,3,1,2,2]
Output
4
Explanation
The distinct values in the whole array are {1, 2, 3}. The complete subarrays are [1,3,1,2], [1,3,1,2,2], [3,1,2], and [3,1,2,2].
Example 2
Input
nums = [5,5,5]
Output
6
Explanation
There is only one distinct value in the array, so every non-empty subarray is complete. The total number of subarrays is 3 + 2 + 1 = 6.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.