Related Articles
Minimum number of distinct elements present in a K-length subsequence in an array
• Difficulty Level : Easy
• Last Updated : 23 Mar, 2021

Given an array A[] consisting of N integers and an integer K, the task is to count the minimum number of distinct elements present in a subsequence of length K of the given array, A.

Examples:

Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 4
Output: 2
Explanation: The subsequence of length 4 containing minimum number of distinct elements is {3, 3, 3, 4}, consisting of 2 distinct elements, i.e. {3, 4}.

Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 5
Output: 2
Explanation: The subsequence of length 5 containing minimum number of distinct elements is {3, 3, 3, 4, 4}, consisting of 2 distinct elements, i.e. {3, 4}.

Naive Approach: The simplest approach is to generate all subsequences of length K and for each subsequence, find the number of distinct elements present in them. Finally, print the minimum number of distinct elements present.

Time Complexity: O(K * NK)
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized using Hashing. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count the minimum number``// of distinct elements present in any``// subsequence of length K of the given array``void` `findMinimumDistinct(``int` `A[], ``int` `N, ``int` `K)``{` `    ``// Stores the frequency``    ``// of each array element``    ``unordered_map<``int``, ``int``> mp;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++)` `        ``// Update frequency``        ``// of array elements``        ``mp[A[i]]++;` `    ``// Store the required result``    ``int` `count = 0;` `    ``// Store the length of the``    ``// required subsequence``    ``int` `len = 0;` `    ``// Store the frequencies``    ``// in decreasing order``    ``vector<``int``> counts;` `    ``// Travere the map``    ``for` `(``auto` `i : mp)` `        ``// Push the frequencies``        ``// into the HashMap``        ``counts.push_back(i.second);` `    ``// Sort the array in decreasing order``    ``sort(counts.begin(), counts.end(),``         ``greater<``int``>());` `    ``// Add the elements into the subsequence``    ``// starting from one with highest frequency``    ``for` `(``int` `i = 0; i < counts.size(); i++) {` `        ``// If length of subsequence is >= k``        ``if` `(len >= K)``            ``break``;``        ``len += counts[i];``        ``count++;``    ``}` `    ``// Print the result``    ``cout << count;``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 3, 1, 3, 2, 3, 4, 5, 4 };``    ``int` `K = 4;` `    ``// Store the size of the array``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``// Function Call to count minimum``    ``// number of distinct elmeents``    ``// present in a K-length subsequence``    ``findMinimumDistinct(A, N, K);` `    ``return` `0;``}`

## Python3

 `# Python3 program for the above approach``from` `collections ``import` `Counter` `# Function to count the minimum number``# of distinct elements present in any``# subsequence of length K of the given array``def` `findMinimumDistinct(A, N, K):``    ` `    ``# Stores the frequency``    ``# of each array element``    ``mp ``=` `Counter(A)``    ` `    ``# Store the required result``    ``count ``=` `0``    ` `    ``# Store the length of the``    ``# required subsequence``    ``length ``=` `0``    ` `    ``# Store the frequencies``    ``# in decreasing order``    ``counts ``=` `[]``    ` `    ``# Travere the map``    ``for` `i ``in` `mp:``        ` `        ``# Push the frequencies``        ``# into the HashMap``        ``counts.append(mp[i])``        ` `    ``# Sort the array in decreasing order``    ``counts ``=` `sorted``(counts)``    ``counts.reverse()``    ` `    ``# Add the elements into the subsequence``    ``# starting from one with highest frequency``    ``for` `i ``in` `range``(``len``(counts)):``        ` `        ``# If length of subsequence is >= k``        ``if` `(length >``=` `K):``            ``break``        ` `        ``length ``+``=` `counts[i]``        ``count ``+``=` `1``        ` `    ``# Print the result``    ``print``(count)` `# Driver Code``A ``=` `[``3``, ``1``, ``3``, ``2``, ``3``, ``4``, ``5``, ``4``]``K ``=` `4` `# Store the size of the array``N ``=` `len``(A)` `# Function Call to count minimum``# number of distinct elmeents``# present in a K-length subsequence``findMinimumDistinct(A, N, K)` `# This code is contributed by sudhanshugupta2019a`
Output:
`2`

Time Complexity: O(N*log(N))
Auxiliary Space: 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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up