 GeeksforGeeks App
Open App Browser
Continue

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

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=3
Output: 2
Explanation: 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=3
Output: 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:

1. Create a set S, which stores the distinct integers present in the array arr[].
2. Traverse the array arr[] and insert each number in the set S.
3. Return the minimum of K and the size of S which is the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach``#include ``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 Code``int` `main()``{``    ``vector<``int``> arr = { 1, 1, 2, 2 };``    ``int` `K = 3;``    ``cout << maxUnique(arr, K);``}`

## Java

 `// Java code for the above approach``import` `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 S = ``new` `HashSet();` `    ``// 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 Code``public` `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`

## Python3

 `# 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 Code``arr ``=` `[``1``, ``1``, ``2``, ``2``]``K ``=` `3``print``(maxUnique(arr, K))` `# This code is contributed by gfgking`

## C#

 `// C# implementation for the above approach``using` `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 Code``public` `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.`

## Javascript

 ``

Output

`2`

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

My Personal Notes arrow_drop_up