Maximize the count of distinct elements in Array after at most K changes
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 modies 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++
// C++ program for above approach#include <bits/stdc++.h>using namespace std; // Function to calculate// maximum distinct elements possible// after at most K changesint maxDistinctElements(int* arr, int K, int L, int R, int n){ // Map to store frequency of all the elements unordered_map<int, int> frequency; // Count frequency of each element for (int x = 0; x < n; x++) { frequency[arr[x]] += 1; } // To store number of extra elements // that needs to be changed int extra = (n - frequency.size()); // Traverse from L to R // and see which number is not // present in map, use that number // to change extra duplicate element for (int i = L; i <= R and K != 0 and extra != 0; i++) { if (!frequency[i]) { frequency[i] = 1; K--; extra--; } } // Total distinct element will be equal // to the size of updated frequency map. int ans = frequency.size(); // Return answer return ans;} // Driver Codeint main(){ // Test case 1 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; // Test case 2 K = 1; cout << maxDistinctElements(arr, K, L, R, N) << endl;} |
6 5
Time Complexity: O(N).
Auxiliary Space: O(N).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


