Maximize count of distinct elements in a subsequence of size K in given array

14 Dec, 2021
Given an array arr[] of N integers and an integer K, the task is to find the maximum count of distinct elements over all the subsequences of K integers.
Example:
Input: arr[]={1, 1, 2, 2}, K=3Output: 2Explanation: The subsequence {1, 1, 2} has 3 integers and the number of distinct integers in it are 2 which is the maximum possible. Other possible subsequence is {1, 2, 2}.
Input: arr[]={1, 2, 3, 4}, K=3Output: 3
Approach: The given problem can be solved using a greedy approach using the observation that the required answer is the minimum of the count of the unique elements in the given array or K. Now, to solve this problem, follow the below steps:
Create a set S, which stores the distinct integers present in the array arr[].Traverse the array arr[] and insert each number in the set S.Return the minimum of K and the size of S which is the required answer.
Create a set S, which stores the distinct integers present in the array arr[].
Traverse the array arr[] and insert each number in the set S.
Return the minimum of K and the size of S which is the required answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ code for the above approach#include <bits/stdc++.h>using namespace std; // Function to find count of maximum// distinct elements in a subsequence// of size K of the given array arr[]int maxUnique(vector<int>& arr, int K){    // Set data structure to    // store the unique elements    unordered_set<int> S;     // Loop to traverse the given array    for (auto x : arr) {         // Insert into the set        S.insert(x);    }     // Returning the minimum out of the two    return min(K, (int)S.size());} // Driver Codeint main(){    vector<int> arr = { 1, 1, 2, 2 };    int K = 3;    cout << maxUnique(arr, K);}
// Java code for the above approachimport java.util.*; class GFG{ // Function to find count of maximum// distinct elements in a subsequence// of size K of the given array arr[]static int maxUnique(int []arr, int K){       // Set data structure to    // store the unique elements    HashSet<Integer> S = new HashSet<Integer>();     // Loop to traverse the given array    for (int x : arr) {         // Insert into the set        S.add(x);    }     // Returning the minimum out of the two    return Math.min(K, (int)S.size());} // Driver Codepublic static void main(String[] args){    int[] arr = { 1, 1, 2, 2 };    int K = 3;    System.out.print(maxUnique(arr, K));}} // This code is contributed by 29AjayKumar
# Python Program to implement# the above approach # Function to find count of maximum# distinct elements in a subsequence# of size K of the given array arr[]def maxUnique(arr, K):     # Set data structure to    # store the unique elements    S = set()     # Loop to traverse the given array    for x in arr:         # Insert into the set        S.add(x)     # Returning the minimum out of the two    return min(K, len(S)) # Driver Codearr = [1, 1, 2, 2]K = 3print(maxUnique(arr, K)) # This code is contributed by gfgking
// C# implementation for the above approachusing System;using System.Collections;using System.Collections.Generic; class GFG{ // Function to find count of maximum// distinct elements in a subsequence// of size K of the given array arr[]static int maxUnique(int []arr, int K){    // Set data structure to    // store the unique elements    HashSet<int> S = new HashSet<int>();     // Loop to traverse the given array    foreach (int x in arr) {         // Insert into the set        S.Add(x);    }     // Returning the minimum out of the two    return Math.Min(K, (int)S.Count);} // Driver Codepublic static void Main(){    int []arr = { 1, 1, 2, 2 };    int K = 3;    Console.Write(maxUnique(arr, K));}} // This code is contributed by Samim Hossain Mondal.
<script>      // JavaScript Program to implement     // the above approach      // Function to find count of maximum     // distinct elements in a subsequence     // of size K of the given array arr[]     function maxUnique(arr, K)     {               // Set data structure to         // store the unique elements         let S = new Set();          // Loop to traverse the given array         for (let x of arr) {              // Insert into the set             S.add(x);         }          // Returning the minimum out of the two         return Math.min(K, S.size);     }      // Driver Code     let arr = [1, 1, 2, 2];     let K = 3;     document.write(maxUnique(arr, K));  // This code is contributed by Potta Lokesh </script>
  
2
 
Time Complexity: O(N)Auxiliary Space: O(N)
 
lokeshpotta20
samim2000
29AjayKumar
gfgking
subsequence
Arrays
Greedy
Mathematical
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.