Given an array arr[] consisting of N positive integers and an integer K, the task is to check if it is possible to reduce the size of the array to at most K or not by removing a subset of the distinct array elements. If it is possible, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {2, 2, 2, 3}, K = 3
Output: Yes
Explanation:
By removing the subset {2, 3}, the array modifies to {2, 2} (Size = 2).Input: arr[] = {1, 1, 1, 3}, K = 1
Output: No
Approach: The given problem can be solved by finding the number of distinct elements in the given array, say count. If the value of (N – count) is at most K, then print Yes. Otherwise, print No.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if it is possible // to reduce the size of the array to K // by removing the set of the distinct // array elements void maxCount( int arr[], int N, int K)
{ // Stores all distinct elements
// present in the array arr[]
set< int > st;
// Traverse the given array
for ( int i = 0; i < N; i++) {
// Insert array elements
// into the set
st.insert(arr[i]);
}
// Condition for reducing size
// of the array to at most K
if (N - st.size() <= K) {
cout << "Yes" ;
}
else
cout << "No" ;
} // Driver Code int main()
{ int arr[] = { 2, 2, 2, 3 };
int K = 3;
int N = sizeof (arr) / sizeof (arr[0]);
maxCount(arr, N, K);
return 0;
} |
// Java program for the above approach import java.util.HashSet;
public class GFG
{ // Function to check if it is possible
// to reduce the size of the array to K
// by removing the set of the distinct
// array elements
static void maxCount( int arr[], int N, int K)
{
// Stores all distinct elements
// present in the array arr[]
HashSet<Integer> st = new HashSet<>();
// Traverse the given array
for ( int i = 0 ; i < N; i++) {
// Insert array elements
// into the set
st.add(arr[i]);
}
// Condition for reducing size
// of the array to at most K
if (N - st.size() <= K) {
System.out.println( "Yes" );
}
else
System.out.println( "No" );
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 2 , 3 };
int K = 3 ;
int N = arr.length;
maxCount(arr, N, K);
}
} // This code is contributed by abhinavjain194 |
# Python 3 program for the above approach # Function to check if it is possible # to reduce the size of the array to K # by removing the set of the distinct # array elements def maxCount(arr, N, K):
# Stores all distinct elements
# present in the array arr[]
st = set ()
# Traverse the given array
for i in range (N):
# Insert array elements
# into the set
st.add(arr[i])
# Condition for reducing size
# of the array to at most K
if (N - len (st) < = K):
print ( "Yes" )
else :
print ( "No" )
# Driver Code if __name__ = = '__main__' :
arr = [ 2 , 2 , 2 , 3 ]
K = 3
N = len (arr)
maxCount(arr, N, K)
# This code is contributed by bgangwar59.
|
// C# program for the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to check if it is possible // to reduce the size of the array to K // by removing the set of the distinct // array elements static void maxCount( int [] arr, int N, int K)
{ // Stores all distinct elements
// present in the array arr[]
HashSet< int > st = new HashSet< int >();
// Traverse the given array
for ( int i = 0; i < N; i++)
{
// Insert array elements
// into the set
st.Add(arr[i]);
}
// Condition for reducing size
// of the array to at most K
if (N - st.Count <= K)
{
Console.Write( "Yes" );
}
else
Console.Write( "No" );
} // Driver code static public void Main()
{ int [] arr = { 2, 2, 2, 3 };
int K = 3;
int N = arr.Length;
maxCount(arr, N, K);
} } // This code is contributed by offbeat |
<script> // JavaScript program for the above approach // Function to check if it is possible // to reduce the size of the array to K // by removing the set of the distinct // array elements function maxCount(arr, N, K) {
// Stores all distinct elements
// present in the array arr[]
let st = new Set();
// Traverse the given array
for (let i = 0; i < N; i++) {
// Insert array elements
// into the set
st.add(arr[i]);
}
// Condition for reducing size
// of the array to at most K
if (N - st.size <= K) {
document.write( "Yes" );
}
else
document.write( "No" );
} // Driver Code let arr = [2, 2, 2, 3]; let K = 3; let N = arr.length maxCount(arr, N, K); </script> |
Yes
Time Complexity: O(N * log N)
Auxiliary Space: O(N)