Find minimum changes required in an array for it to contain k distinct elements
Last Updated :
28 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
int Min_Replace( int arr[], int n, int k)
{
sort(arr, arr + n);
int freq[MAX];
memset (freq, 0, sizeof freq);
int p = 0;
freq[p] = 1;
for ( int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
++freq[p];
else
++freq[++p];
}
sort(freq, freq + n, greater< int >());
int ans = 0;
for ( int i = k; i <= p; i++)
ans += freq[i];
return ans;
}
int 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
import java.util.*;
class GFG
{
static int MAX = 100005 ;
static int Min_Replace( int [] arr,
int n, int k)
{
Arrays.sort(arr);
Integer [] freq = new Integer[MAX];
Arrays.fill(freq, 0 );
int p = 0 ;
freq[p] = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[i] == arr[i - 1 ])
++freq[p];
else
++freq[++p];
}
Arrays.sort(freq, Collections.reverseOrder());
int ans = 0 ;
for ( int i = k; i <= p; i++)
ans += freq[i];
return ans;
}
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));
}
}
|
Python3
MAX = 100005
def Min_Replace(arr, n, k):
arr.sort(reverse = False )
freq = [ 0 for i in range ( MAX )]
p = 0
freq[p] = 1
for i in range ( 1 , n, 1 ):
if (arr[i] = = arr[i - 1 ]):
freq[p] + = 1
else :
p + = 1
freq[p] + = 1
freq.sort(reverse = True )
ans = 0
for i in range (k, p + 1 , 1 ):
ans + = freq[i]
return ans
if __name__ = = '__main__' :
arr = [ 1 , 2 , 7 , 8 , 2 , 3 , 2 , 3 ]
n = len (arr)
k = 2
print (Min_Replace(arr, n, k))
|
C#
using System;
class GFG
{
static int MAX = 100005;
static int Min_Replace( int [] arr,
int n, int k)
{
Array.Sort(arr);
int [] freq = new int [MAX];
int p = 0;
freq[p] = 1;
for ( int i = 1; i < n; i++)
{
if (arr[i] == arr[i - 1])
++freq[p];
else
++freq[++p];
}
Array.Sort(freq);
Array.Reverse(freq);
int ans = 0;
for ( int i = k; i <= p; i++)
ans += freq[i];
return ans;
}
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));
}
}
|
Javascript
<script>
var MAX = 100005;
function Min_Replace(arr, n, k)
{
arr.sort((a,b)=>a-b)
var freq = Array(MAX).fill(0);
var p = 0;
freq[p] = 1;
for ( var i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
++freq[p];
else
++freq[++p];
}
freq.sort((a,b)=>b-a);
var ans = 0;
for ( var i = k; i <= p; i++)
ans += freq[i];
return ans;
}
var 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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...