Find minimum changes required in an array for it to contain k distinct elements

Given an array arr of size N and a number K. The task is to find the minimum elements to be replaced in the array with any number such that the array consists of K distinct elements.
Note: The array might consist of repeating elements.Â
Examples:Â
Â
Input : arr[]={1, 2, 2, 8}, k = 1Â
Output : 2Â
The elements to be changed are 1, 8
Input : arr[]={1, 2, 7, 8, 2, 3, 2, 3}, k = 2Â
Output : 3Â
The elements to be changed are 1, 7, 8Â
Â
Â
Approach: Since the task is to replace minimum elements from the array so we won’t replace elements which have more frequency in the array. So just define an array freq[] which stores the frequency of each number present in the array arr, then sort freq in descending order. So, first k elements of freq array don’t need to be replaced.
Below is the implementation of the above approach :Â
Â
C++
// CPP program to minimum changes required // in an array for k distinct elements.#include <bits/stdc++.h>using namespace std;Â
#define MAX 100005Â
// Function to minimum changes required // in an array for k distinct elements.int Min_Replace(int arr[], int n, int k){Â Â Â Â sort(arr, arr + n);Â
    // Store the frequency of each element    int freq[MAX];         memset(freq, 0, sizeof freq);         int p = 0;    freq[p] = 1;         // Store the frequency of elements    for (int i = 1; i < n; i++) {        if (arr[i] == arr[i - 1])            ++freq[p];        else            ++freq[++p];    }Â
    // Sort frequencies in descending order    sort(freq, freq + n, greater<int>());         // To store the required answer    int ans = 0;    for (int i = k; i <= p; i++)        ans += freq[i];             // Return the required answer    return ans;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 2, 7, 8, 2, 3, 2, 3 };Â Â Â Â Â Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â Â Â Â Â Â int k = 2;Â Â Â Â Â Â Â Â Â cout << Min_Replace(arr, n, k);Â Â Â Â Â Â Â Â Â return 0;} |
Java
// C# program to minimum changes required // in an array for k distinct elements.import java.util.*;Â
class GFG{    static int MAX = 100005;         // Function to minimum changes required     // in an array for k distinct elements.    static int Min_Replace(int [] arr,                            int n, int k)    {        Arrays.sort(arr);             // Store the frequency of each element        Integer [] freq = new Integer[MAX];        Arrays.fill(freq, 0);        int p = 0;        freq[p] = 1;                 // Store the frequency of elements        for (int i = 1; i < n; i++)        {            if (arr[i] == arr[i - 1])                ++freq[p];            else                ++freq[++p];        }             // Sort frequencies in descending order        Arrays.sort(freq, Collections.reverseOrder());                 // To store the required answer        int ans = 0;        for (int i = k; i <= p; i++)            ans += freq[i];                     // Return the required answer        return ans;    }         // Driver code    public static void main (String []args)    {        int [] arr = { 1, 2, 7, 8, 2, 3, 2, 3 };                 int n = arr.length;                 int k = 2;                 System.out.println(Min_Replace(arr, n, k));    }}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python 3 program to minimum changes required # in an array for k distinct elements.MAX = 100005Â
# Function to minimum changes required # in an array for k distinct elements.def Min_Replace(arr, n, k):Â Â Â Â arr.sort(reverse = False)Â
    # Store the frequency of each element    freq = [0 for i in range(MAX)]         p = 0    freq[p] = 1         # Store the frequency of elements    for i in range(1, n, 1):        if (arr[i] == arr[i - 1]):            freq[p] += 1        else:            p += 1            freq[p] += 1Â
    # Sort frequencies in descending order    freq.sort(reverse = True)         # To store the required answer    ans = 0    for i in range(k, p + 1, 1):        ans += freq[i]             # Return the required answer    return ansÂ
# Driver codeif __name__ == '__main__':Â Â Â Â arr = [1, 2, 7, 8, 2, 3, 2, 3]Â Â Â Â Â Â Â Â Â n = len(arr)Â Â Â Â Â Â Â Â Â k = 2Â Â Â Â Â Â Â Â Â print(Min_Replace(arr, n, k))Â Â Â Â Â # This code is contributed by# Surendra_Gangwar |
C#
// C# program to minimum changes required // in an array for k distinct elements.using System;Â
class GFG{    static int MAX = 100005;         // Function to minimum changes required     // in an array for k distinct elements.    static int Min_Replace(int [] arr,                            int n, int k)    {        Array.Sort(arr);             // Store the frequency of each element        int [] freq = new int[MAX];                 int p = 0;        freq[p] = 1;                 // Store the frequency of elements        for (int i = 1; i < n; i++)        {            if (arr[i] == arr[i - 1])                ++freq[p];            else                ++freq[++p];        }             // Sort frequencies in descending order        Array.Sort(freq);        Array.Reverse(freq);                 // To store the required answer        int ans = 0;        for (int i = k; i <= p; i++)            ans += freq[i];                     // Return the required answer        return ans;    }         // Driver code    public static void Main ()    {        int [] arr = { 1, 2, 7, 8, 2, 3, 2, 3 };                 int n = arr.Length;                 int k = 2;                 Console.WriteLine(Min_Replace(arr, n, k));    }}Â
// This code is contributed by ihritik |
Javascript
<script>Â
// Javascript program to minimum changes required // in an array for k distinct elements.Â
var MAX = 100005;Â
// Function to minimum changes required // in an array for k distinct elements.function Min_Replace(arr, n, k){Â Â Â Â arr.sort((a,b)=>a-b)Â
    // Store the frequency of each element    var freq = Array(MAX).fill(0);         var p = 0;    freq[p] = 1;         // Store the frequency of elements    for (var i = 1; i < n; i++) {        if (arr[i] == arr[i - 1])            ++freq[p];        else            ++freq[++p];    }Â
    // Sort frequencies in descending order    freq.sort((a,b)=>b-a);         // To store the required answer    var ans = 0;    for (var i = k; i <= p; i++)        ans += freq[i];             // Return the required answer    return ans;}Â
// Driver codevar arr = [1, 2, 7, 8, 2, 3, 2, 3];var n = arr.length;var k = 2;document.write( Min_Replace(arr, n, k));Â
Â
</script> |
Output:Â
Â
3
Time Complexity : O(NlogN)
Auxiliary Space: O(1) because it is using constant size freq array
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



