Maximize count of distinct elements in a subsequence of size K in given array
Last Updated :
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=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:
- 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++
// 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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count of subsequences having maximum distinct elements Given an arr of size n. The problem is to count all the subsequences having maximum number of distinct elements. Examples: Input : arr[] = {4, 7, 6, 7} Output : 2 The indexes for the subsequences are: {0, 1, 2} - Subsequence is {4, 7, 6} and {0, 2, 3} - Subsequence is {4, 6, 7} Input : arr[] = {9, 6
5 min read
Minimum number of distinct elements present in a K-length subsequence in an array 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 = 4Output: 2Explanation: The subsequence of length 4 containing mini
7 min read
Maximize the count of distinct elements in Array after at most K changes Given an array arr[], the task is to find the maximum number of distinct numbers in arr after at most K changes. In each change pick any element X from arr and change it to Y such that L <= Y <= R. Examples: Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 2Output: 6Explanation: Foll
8 min read
Maximum sum of K-length subarray consisting of same number of distinct elements as the given array Given an array arr[] consisting of N integers and an integer K, the task is to find a subarray of size K with maximum sum and count of distinct elements same as that of the original array. Examples: Input: arr[] = {7, 7, 2, 4, 2, 7, 4, 6, 6, 6}, K = 6Output: 31Explanation: The given array consists o
15+ min read
Maximize count of Decreasing Subsequences from the given Array Given an array arr[], the task is to rearrange the array to generate maximum decreasing subsequences and print the count of the maximum number of subsequences possible such that each array element can be part of a single subsequence and the length of the subsequences needs to be maximized. Example:
5 min read
Length of longest subsequence consisting of distinct adjacent elements Given an array arr[], the task is to find the length of the longest subsequence of the array arr[] such that all adjacent elements in the subsequence are different. Examples: Input: arr[] = {4, 2, 3, 4, 3}Output: 5Explanation:The longest subsequence where no two adjacent elements are equal is {4, 2,
5 min read