Distinct Values Subarrays

You are given an array nums of integers. Your task is to calculate the total number of contiguous subarrays where every element inside the subarray is strictly unique (no duplicates).

Because the total number of valid subarrays can be very large, ensure you return the count as a 64-bit integer (long or long long).

Example 1:

Input: nums: [1, 2, 1, 3]

Output: 8

Explanation:

The valid subarrays with entirely distinct elements are:

  • Length 1: [1] (first one), [2], [1] (second one), [3]
  • Length 2: [1, 2], [2, 1], [1, 3]
  • Length 3: [2, 1, 3] Total count = 4 + 3 + 1 = 8.

Example 2:

Input: nums: [5, 5, 5]

Output: 3

Explanation:

The only valid subarrays are the individual elements themselves: [5] (index 0), [5] (index 1), and [5] (index 2). Any subarray of length 2 or 3 will contain duplicate 5s. Total count = 3.

Now Your Turn!

Pick the correct output for the given input

Input: nums: [1, 2, 3, 5]

(What is the total number of distinct subarrays for this perfectly unique array?)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 200,000
  • 1 <= nums[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long countDistinctSubarrays(vector<int>& nums) {
}
};
Test Case

Input:

Nums