📌  相关文章
📜  检查是否可以通过删除不同元素将数组减少到最大长度 K

📅  最后修改于: 2022-05-13 01:56:05.506000             🧑  作者: Mango

检查是否可以通过删除不同元素将数组减少到最大长度 K

给定一个由N个正整数和一个整数K组成的数组arr[] ,任务是检查是否可以通过删除不同数组元素的子集来将数组的大小减少到最多 K。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:给定的问题可以通过查找给定数组中不同元素的数量来解决,比如count 。如果(N – count)的值最多为 K ,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 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
// 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 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


Python3
# 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#
// 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 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.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


Javascript


输出:
Yes

时间复杂度: O(N * log N)
辅助空间: O(N)