2799. Count Complete Subarrays in an Array LeetCode Solution

In this guide, you will get 2799. Count Complete Subarrays in an Array LeetCode Solution with the best time and space complexity. The solution to Count Complete Subarrays in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Complete Subarrays in an Array solution in C++
  4. Count Complete Subarrays in an Array solution in Java
  5. Count Complete Subarrays in an Array solution in Python
  6. Additional Resources
2799. Count Complete Subarrays in an Array LeetCode Solution image

Problem Statement of Count Complete Subarrays in an Array

You are given an array nums consisting of positive integers.
We call a subarray of an array complete if the following condition is satisfied:

The number of distinct elements in the subarray is equal to the number of distinct elements in the whole array.

Return the number of complete subarrays.
A subarray is a contiguous non-empty part of an array.

Example 1:

Input: nums = [1,3,1,2,2]
Output: 4
Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2].

Example 2:

Input: nums = [5,5,5,5]
Output: 10
Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10.

Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 2000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2799. Count Complete Subarrays in an Array LeetCode Solution in C++

class Solution {
 public:
  int countCompleteSubarrays(vector<int>& nums) {
    constexpr int kMax = 2000;
    const int totalDistinct =
        unordered_set<int>(nums.begin(), nums.end()).size();
    int ans = 0;
    int distinct = 0;
    vector<int> count(kMax + 1);

    int l = 0;
    for (const int num : nums) {
      if (++count[num] == 1)
        ++distinct;
      while (distinct == totalDistinct)
        if (--count[nums[l++]] == 0)
          --distinct;
      // Assume nums[r] = num,
      // nums[0..r], nums[1..r], ..., nums[l - 1..r] have k different values.
      ans += l;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2799. Count Complete Subarrays in an Array LeetCode Solution in Java

class Solution {
  public int countCompleteSubarrays(int[] nums) {
    final int kMax = 2000;
    final int totalDistinct = Arrays.stream(nums).boxed().collect(Collectors.toSet()).size();
    int ans = 0;
    int distinct = 0;
    int[] count = new int[kMax + 1];

    int l = 0;
    for (final int num : nums) {
      if (++count[num] == 1)
        ++distinct;
      while (distinct == totalDistinct)
        if (--count[nums[l++]] == 0)
          --distinct;
      // Assume nums[r] = num,
      // nums[0..r], nums[1..r], ..., nums[l - 1..r] have k different values.
      ans += l;
    }

    return ans;
  }
}
// code provided by PROGIEZ

2799. Count Complete Subarrays in an Array LeetCode Solution in Python

class Solution:
  def countCompleteSubarrays(self, nums: list[int]) -> int:
    ans = 0
    distinct = len(set(nums))
    count = collections.Counter()

    l = 0
    for num in nums:
      count[num] += 1
      while len(count) == distinct:
        count[nums[l]] -= 1
        if count[nums[l]] == 0:
          del count[nums[l]]
        l += 1
      # Assume nums[r] = num,
      # nums[0..r], nums[1..r], ..., nums[l - 1..r] have k different values.
      ans += l

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.