Maximize the count of distinct elements in Array after at most K changes
Last Updated :
18 May, 2022
Given an array arr[], the task is to find the maximum number of distinct numbers in arr after at most K changes. In each change pick any element X from arr and change it to Y such that L <= Y <= R.
Examples:
Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 2
Output: 6
Explanation:
Following are the operations performed on the given array elements:
1. Changing arr[2] to 3 modifies array to {1, 2, 3, 4, 6, 4, 4}
2. Changing arr[3] to 5 modifies array to {1, 2, 3, 5, 6, 4, 4}
As K = 2, no more changes can be done
Therefore, Distinct elements are {1, 2, 3, 5, 6, 4} and the number of maximum distinct elements is 6.
Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 1
Output: 5
Explanation:
Following are the operations performed on the given array elements:
1. Changing arr[2] to 3 modifies array to {1, 2, 3, 4, 6, 4, 4}
As K = 1, no more changes can be done
Therefore, Distinct elements are {1, 2, 3, 6, 4} and the number of maximum distinct elements is 5.
Approach: This problem can be solved by using an unordered map. Store frequency of all the elements in the map and then traverse from L to R, see which number is not already present in the map we can use that number to replace any duplicate element in arr.
- At first, store the frequency of all the elements in the map.
- Total distinct elements will be equal to the size of the map.
- The number of extra elements will be equal to the (size of array – size of map).
- Then iterate from L to R and check how many elements inside this range can be used to replace the extra elements present in the array.
- Update the map for each change in any extra element to any other value.
- At last, the size of the map will be containing all the distinct elements.
- Return the size of the map as the answer.
C++
#include <bits/stdc++.h>
using namespace std;
int maxDistinctElements( int * arr, int K,
int L, int R, int n)
{
unordered_map< int , int > frequency;
for ( int x = 0; x < n; x++) {
frequency[arr[x]] += 1;
}
int extra = (n - frequency.size());
for ( int i = L;
i <= R and K != 0 and extra != 0;
i++) {
if (!frequency[i]) {
frequency[i] = 1;
K--;
extra--;
}
}
int ans = frequency.size();
return ans;
}
int main()
{
int N = 7, L = 1, R = 5, K = 2;
int arr[7] = { 1, 2, 1, 4, 6, 4, 4 };
cout << maxDistinctElements(arr, K, L, R, N)
<< endl;
K = 1;
cout << maxDistinctElements(arr, K, L, R, N)
<< endl;
}
|
Java
import java.util.*;
class GFG {
static int maxDistinctElements( int [] arr, int K, int L, int R, int n)
{
HashMap<Integer, Integer> frequency = new HashMap<Integer, Integer>();
for ( int x = 0 ; x < n; x++) {
if (frequency.containsKey(arr[x])) {
frequency.put(arr[x], frequency.get(arr[x]) + 1 );
} else {
frequency.put(arr[x], 1 );
}
}
int extra = (n - frequency.size());
for ( int i = L; i <= R && K != 0 && extra != 0 ; i++) {
if (!frequency.containsKey(i)) {
frequency.put(i, 1 );
K--;
extra--;
}
}
int ans = frequency.size();
return ans;
}
public static void main(String[] args) {
int N = 7 , L = 1 , R = 5 , K = 2 ;
int arr[] = { 1 , 2 , 1 , 4 , 6 , 4 , 4 };
System.out.print(maxDistinctElements(arr, K, L, R, N) + "\n" );
K = 1 ;
System.out.print(maxDistinctElements(arr, K, L, R, N) + "\n" );
}
}
|
Python3
def maxDistinctElements(arr, K, L, R, n):
frequency = {}
for x in range (n):
if arr[x] in frequency:
frequency[arr[x]] + = 1
else :
frequency[arr[x]] = 1
extra = (n - len (frequency))
i = L
while (i < = R and K ! = 0 and extra ! = 0 ):
if (i not in frequency):
frequency[i] = 1
K - = 1
extra - = 1
else :
frequency[i] = 1
i + = 1
ans = len (frequency)
return ans
if __name__ = = '__main__' :
N = 7
L = 1
R = 5
K = 2
arr = [ 1 , 2 , 1 , 4 , 6 , 4 , 4 ]
print (maxDistinctElements(arr, K, L, R, N))
K = 1
print (maxDistinctElements(arr, K, L, R, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int maxDistinctElements( int [] arr, int K, int L,
int R, int n)
{
Dictionary< int , int > frequency
= new Dictionary< int , int >();
for ( int x = 0; x < n; x++) {
if (frequency.ContainsKey(arr[x]))
frequency[arr[x]] += 1;
else
frequency[arr[x]] = 1;
}
int extra = (n - frequency.Count);
for ( int i = L; i <= R && K != 0 && extra != 0;
i++) {
if (!frequency.ContainsKey(i)) {
frequency[i] = 1;
K--;
extra--;
}
}
int ans = frequency.Count;
return ans;
}
public static void Main()
{
int N = 7, L = 1, R = 5, K = 2;
int [] arr = { 1, 2, 1, 4, 6, 4, 4 };
Console.WriteLine(
maxDistinctElements(arr, K, L, R, N));
K = 1;
Console.WriteLine(
maxDistinctElements(arr, K, L, R, N));
}
}
|
Javascript
<script>
function maxDistinctElements(arr, K, L, R, n) {
let frequency = new Map();
for (let x = 0; x < n; x++) {
if (frequency.has(arr[x])) {
frequency.set(frequency.get(arr[x]), frequency.get(arr[x]) + 1);
}
else {
frequency.set(arr[x], 1)
}
}
let extra = (n - frequency.size);
for (let i = L;
i <= R && K != 0 && extra != 0;
i++) {
if (!frequency.has(i)) {
frequency.set(i, 1);
K--;
extra--;
}
}
let ans = frequency.size;
return ans;
}
let N = 7, L = 1, R = 5, K = 2;
let arr = [1, 2, 1, 4, 6, 4, 4];
document.write(maxDistinctElements(arr, K, L, R, N) + "<br>" );
K = 1;
document.write(maxDistinctElements(arr, K, L, R, N) + "<br>" );
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(N).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...