Verifique si una array se puede reducir a una longitud máxima K mediante la eliminación de elementos distintos

Dada una array arr[] que consta de N enteros positivos y un entero K , la tarea es verificar si es posible reducir el tamaño de la array a un máximo de K o no eliminando un subconjunto de los distintos elementos de la array. Si es posible, escriba «Sí» . De lo contrario, escriba “No” .

Ejemplos:

Entrada: arr[] = {2, 2, 2, 3}, K = 3
Salida:
Explicación:
Al eliminar el subconjunto {2, 3}, la array se modifica a {2, 2} (Tamaño = 2).

Entrada: arr[] = {1, 1, 1, 3}, K = 1
Salida: No

Enfoque: el problema dado se puede resolver encontrando la cantidad de elementos distintos en la array dada , digamos contar . Si el valor de (N – recuento) es como máximo K , imprima . De lo contrario , imprima No.

A continuación se muestra la implementación del enfoque anterior:

C++

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

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

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

Javascript

<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>
Producción: 

Yes

 

Complejidad de tiempo: O(N * log N)
Espacio auxiliar: O(N)

Publicación traducida automáticamente

Artículo escrito por dharanendralv23 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *