Minimum number of distinct elements present in a K-length subsequence in an array
Last Updated :
30 Jan, 2023
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:
- Store the frequencies of all elements in the given array, A[] in a HashMap, say M.
- Traverse the hashmap, M and push the frequencies in another array, say V.
- Sort the array V in decreasing order.
- Initialize two variables, cnt and len as 0, to store the required result and the length of the subsequence thus formed.
- Traverse the array V[] using a variable, say i
- If the value of len ? K, then break out of the loop.
- Otherwise, increment the value of len by V[i] and cnt by 1.
- After completing the above steps, print the value of cnt as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumDistinct( int A[], int N, int K)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++)
mp[A[i]]++;
int count = 0;
int len = 0;
vector< int > counts;
for ( auto i : mp)
counts.push_back(i.second);
sort(counts.begin(), counts.end(),
greater< int >());
for ( int i = 0; i < counts.size(); i++) {
if (len >= K)
break ;
len += counts[i];
count++;
}
cout << count;
}
int main()
{
int A[] = { 3, 1, 3, 2, 3, 4, 5, 4 };
int K = 4;
int N = sizeof (A) / sizeof (A[0]);
findMinimumDistinct(A, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMinimumDistinct( int A[], int N, int K)
{
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++)
mp.put(A[i], mp.getOrDefault(A[i], 0 ) + 1 );
int count = 0 ;
int len = 0 ;
ArrayList<Integer> counts = new ArrayList<>();
for (Map.Entry<Integer, Integer> i : mp.entrySet())
counts.add(i.getValue());
Collections.sort(counts, (a, b) -> b - a);
for ( int i = 0 ; i < counts.size(); i++)
{
if (len >= K)
break ;
len += counts.get(i);
count++;
}
System.out.print(count);
}
public static void main(String[] args)
{
int A[] = { 3 , 1 , 3 , 2 , 3 , 4 , 5 , 4 };
int K = 4 ;
int N = A.length;
findMinimumDistinct(A, N, K);
}
}
|
Python3
from collections import Counter
def findMinimumDistinct(A, N, K):
mp = Counter(A)
count = 0
length = 0
counts = []
for i in mp:
counts.append(mp[i])
counts = sorted (counts)
counts.reverse()
for i in range ( len (counts)):
if (length > = K):
break
length + = counts[i]
count + = 1
print (count)
A = [ 3 , 1 , 3 , 2 , 3 , 4 , 5 , 4 ]
K = 4
N = len (A)
findMinimumDistinct(A, N, K)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main( string [] args)
{
int [] A = new int [] { 3, 1, 3, 2, 3, 4, 5, 4 };
int K = 4;
int N = A.Length;
Console.WriteLine(FindMinimumDistinct(A, N, K));
}
static int FindMinimumDistinct( int [] A, int N, int K)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
foreach ( var item in A)
{
if (mp.ContainsKey(item))
mp[item]++;
else
mp[item] = 1;
}
int count = 0;
int length = 0;
List< int > counts = new List< int >();
foreach ( var item in mp)
counts.Add(item.Value);
counts.Sort();
counts.Reverse();
for ( int i = 0; i < counts.Count; i++)
{
if (length >= K)
break ;
length += counts[i];
count++;
}
return count;
}
}
|
Javascript
<script>
function findMinimumDistinct(A, N, K)
{
let mp = new Map();
for (let i = 0; i < N; i++){
if (mp.has(A[i])){
mp.set(A[i],mp.get(A[i])+1)
}
else mp.set(A[i],1)
}
let count = 0;
let len = 0;
let counts = [];
for (let [i,j] of mp)
counts.push(j);
counts.sort((a,b)=>b-a);
for (let i = 0; i < counts.length; i++) {
if (len >= K)
break ;
len += counts[i];
count++;
}
document.write(count);
}
let A = [ 3, 1, 3, 2, 3, 4, 5, 4 ];
let K = 4;
let N = A.length;
findMinimumDistinct(A, N, K);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...