Open In App

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

Last Updated : 14 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 <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 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<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 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




<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>


 
 

Output

2

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads