Loading content...
You are given an array of positive integers called nums.
A contiguous subarray is considered "full spectrum" if it contains every distinct value that appears anywhere in the original array. In other words, a subarray qualifies as full spectrum when the count of unique elements within that subarray matches the total count of unique elements in the entire array.
Your task is to determine how many such full spectrum subarrays exist within the given array.
Key Definitions:
For instance, if the original array contains three distinct values {1, 2, 3}, then any contiguous subarray that includes at least one occurrence of each of these three values qualifies as a full spectrum subarray.
nums = [1,3,1,2,2]4The entire array contains 3 distinct values: {1, 2, 3}. The full spectrum subarrays are: • [1,3,1,2] (indices 0-3): contains 1, 3, and 2 ✓ • [1,3,1,2,2] (indices 0-4): contains 1, 3, and 2 ✓ • [3,1,2] (indices 1-3): contains 3, 1, and 2 ✓ • [3,1,2,2] (indices 1-4): contains 3, 1, and 2 ✓ Thus, there are exactly 4 full spectrum subarrays.
nums = [5,5,5,5]10The array contains only one distinct value: {5}. Any non-empty subarray automatically contains all distinct values (just 5), so every possible contiguous subarray is a full spectrum subarray. For an array of length 4, the total number of subarrays is n×(n+1)/2 = 4×5/2 = 10.
nums = [1,2,3]1The array contains 3 distinct values: {1, 2, 3}. The only subarray that contains all three distinct values is the entire array [1,2,3] itself. Any shorter subarray will miss at least one distinct element.
Constraints