Count of subarrays having exactly K distinct elements

Given an array arr[] of size N and an integer K. The task is to find the count of subarrays such that each subarray has exactly K distinct elements.

Examples:

Input: arr[] = {2, 1, 2, 1, 6}, K = 2
Output: 7
{2, 1}, {1, 2}, {2, 1}, {1, 6}, {2, 1, 2},
{1, 2, 1} and {2, 1, 2, 1} are the only valid subarrays.

Input: arr[] = {1, 2, 3, 4, 5}, K = 1
Output: 5

Approach: To directly count the subarrays with exactly K different integers is hard but to find the count of subarrays with at most K different integers is easy. So the idea is to find the count of subarrays with at most K different integers, let it be C(K), and the count of subarrays with at most (K – 1) different integers, let it be C(K – 1) and finally take their difference, C(K) – C(K – 1) which is the required answer.
Count of subarrays with at most K different elements can be easily calculated through the sliding window technique. The idea is to keep expanding the right boundary of the window till the count of distinct elements in the window is less than or equal to K and when the count of distinct elements inside the window becomes more than K, start shrinking the window from the left till the count becomes less than or equal to K. Also for every expansion, keep counting the subarrays as right – left + 1 where right and left are the boundaries of the current window.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
#include<map>
using namespace std;
  
  
// Function to return the count of subarrays
// with at most K distinct elements using
// the sliding window technique
int atMostK(int arr[], int n, int k)
{
  
    // To store the result
    int count = 0;
  
    // Left boundary of window
    int left = 0;
  
    // Right boundary of window
    int right = 0;
  
    // Map to keep track of number of distinct
    // elements in the current window
    map<int,int> map;
    // Loop to calculate the count
    while (right < n) {
  
        // Calculating the frequency of each
        // element in the current window
        if (map.find(arr[right])==map.end())
            map[arr[right]]=0;
        map[arr[right]]++;
  
        // Shrinking the window from left if the
        // count of distinct elements exceeds K
        while (map.size() > k) {
            map[arr[left]]= map[arr[left]] - 1;
            if (map[arr[left]] == 0)
                map.erase(arr[left]);
            left++;
        }
  
        // Adding the count of subarrays with at most
        // K distinct elements in the current window
        count += right - left + 1;
        right++;
    }
    return count;
}
  
// Function to return the count of subarrays
// with exactly K distinct elements
int exactlyK(int arr[], int n, int k)
{
  
    // Count of subarrays with exactly k distinct
    // elements is equal to the difference of the
    // count of subarrays with at most K distinct
    // elements and the count of subararys with
    // at most (K - 1) distinct elements
    return (atMostK(arr, n, k) - atMostK(arr, n, k - 1));
}
  
// Driver code
int main()
{
    int arr[] = { 2, 1, 2, 1, 6 };
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 2;
  
    cout<<(exactlyK(arr, n, k));
}
  
// This code is contributed by chitranayal

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
public class GfG {
  
    // Function to return the count of subarrays
    // with at most K distinct elements using
    // the sliding window technique
    private static int atMostK(int arr[], int n, int k)
    {
  
        // To store the result
        int count = 0;
  
        // Left boundary of window
        int left = 0;
  
        // Right boundary of window
        int right = 0;
  
        // Map to keep track of number of distinct
        // elements in the current window
        HashMap<Integer, Integer> map = new HashMap<>();
  
        // Loop to calculate the count
        while (right < n) {
  
            // Calculating the frequency of each
            // element in the current window
            map.put(arr[right], map.getOrDefault(arr[right], 0) + 1);
  
            // Shrinking the window from left if the
            // count of distinct elements exceeds K
            while (map.size() > k) {
                map.put(arr[left], map.get(arr[left]) - 1);
                if (map.get(arr[left]) == 0)
                    map.remove(arr[left]);
                left++;
            }
  
            // Adding the count of subarrays with at most
            // K distinct elements in the current window
            count += right - left + 1;
            right++;
        }
        return count;
    }
  
    // Function to return the count of subarrays
    // with exactly K distinct elements
    private static int exactlyK(int arr[], int n, int k)
    {
  
        // Count of subarrays with exactly k distinct
        // elements is equal to the difference of the
        // count of subarrays with at most K distinct
        // elements and the count of subararys with
        // at most (K - 1) distinct elements
        return (atMostK(arr, n, k) - atMostK(arr, n, k - 1));
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 1, 2, 1, 6 };
        int n = arr.length;
        int k = 2;
  
        System.out.print(exactlyK(arr, n, k));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to return the count of subarrays 
# with at most K distinct elements using 
# the sliding window technique
def atMostK(arr, n, k) :
      
    # To store the result
    count = 0
      
    # Left boundary of window
    left = 0
      
    # Right boundary of window 
    right = 0
      
    # Map to keep track of number of distinct 
    # elements in the current window 
    map = {}
  
    # Loop to calculate the count 
    while(right < n) :
          
        if arr[right] not in map :
            map[arr[right]] = 0
          
        # Calculating the frequency of each 
        # element in the current window     
        map[arr[right]] += 1
          
        # Shrinking the window from left if the 
        # count of distinct elements exceeds K 
        while(len(map) > k) :
              
            if arr[left] not in map :
                map[arr[left]] = 0
                  
            map[arr[left]] -= 1
              
            if map[arr[left]] == 0 :
                del map[arr[left]]
              
            left += 1
          
        # Adding the count of subarrays with at most 
        # K distinct elements in the current window 
        count += right - left + 1
        right += 1
      
    return count
      
# Function to return the count of subarrays 
# with exactly K distinct elements 
def exactlyK(arr, n, k) :
      
    # Count of subarrays with exactly k distinct 
    # elements is equal to the difference of the 
    # count of subarrays with at most K distinct 
    # elements and the count of subararys with 
    # at most (K - 1) distinct elements 
    return (atMostK(arr, n, k) -
            atMostK(arr, n, k - 1))
  
# Driver code
if __name__ == "__main__" :
    arr = [2, 1, 2, 1, 6]
    n = len(arr)
    k = 2
      
    print(exactlyK(arr, n, k))
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GfG 
{
  
    // Function to return the count of subarrays
    // with at most K distinct elements using
    // the sliding window technique
    private static int atMostK(int []arr, int n, int k)
    {
  
        // To store the result
        int count = 0;
  
        // Left boundary of window
        int left = 0;
  
        // Right boundary of window
        int right = 0;
  
        // Map to keep track of number of distinct
        // elements in the current window
        Dictionary<int, int> map = new Dictionary<int, int>();
  
        // Loop to calculate the count
        while (right < n) 
        {
  
            // Calculating the frequency of each
            // element in the current window
            if(map.ContainsKey(arr[right]))
                map[arr[right]] = map[arr[right]] + 1; 
            else
                map.Add(arr[right], 1);
  
            // Shrinking the window from left if the
            // count of distinct elements exceeds K
            while (map.Count > k)
            {
                if(map.ContainsKey(arr[left])) 
                {
                    map[arr[left]] = map[arr[left]] - 1;
                    if (map[arr[left]] == 0)
                        map.Remove(arr[left]);
                }
                left++;
            }
  
            // Adding the count of subarrays with at most
            // K distinct elements in the current window
            count += right - left + 1;
            right++;
        }
        return count;
    }
  
    // Function to return the count of subarrays
    // with exactly K distinct elements
    private static int exactlyK(int []arr, int n, int k)
    {
  
        // Count of subarrays with exactly k distinct
        // elements is equal to the difference of the
        // count of subarrays with at most K distinct
        // elements and the count of subararys with
        // at most (K - 1) distinct elements
        return (atMostK(arr, n, k) - atMostK(arr, n, k - 1));
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 2, 1, 2, 1, 6 };
        int n = arr.Length;
        int k = 2;
  
        Console.Write(exactlyK(arr, n, k));
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

7

Time Complexity: O(N)
Space Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.