Maximise occurrence of an element after K replacements within Array elements
Given an array arr[] having N integers, and an integer K, the task is to find an array such that it contains a single elements maximum number of times possible after K replacements within array elements.
Examples:
Input: N = 7, arr[] = {1, 2, 1, 5, 1, 6, 7}, K = 3
Output: {1, 1, 1, 1, 1, 1, 7}
Explanation: We can replace element on index 1, 3, and 5 with 1, i.e. A[1] = A[0], A[3] = A[0] and A[5] = A[0]Input: N = 6, arr[] = {2, 2, 2, 5, 1, 6 }, K = 3
Output: {2, 2, 2, 2, 2, 2 }
Approach: The approach to solve this problem is based on following idea:
- If we dont do any replacement, and if there are duplicates present in the array, then there will be an element present already occurring maximum number of times in the array.
- Now when K replacements are allowed, we can simply try to change replace elements not equal to maximum occurring element with maximum occurring element K times, to get the desired array.
Following is the algorithm to implement above discussed approach:
- Create a hashmap to store the frequency of every distinct element.
- Find the maximum occurring element in the array.
- Then assign every element value of maximum occurring element.
- Make sure to take care of frequency before assigning the element and after assigning the element.
- For that first decrease the frequency of the current element and after assigning it the maximum occurring value increase its value in the hashmap.
Below is the implementation of the above approach:
C++
// C++ code for the above discussed approach #include <bits/stdc++.h> using namespace std; // Function to maximizeTheElements vector< int > maximizeTheElements( int N, vector< int > arr, int K) { // Map to store the frequency // of the elements unordered_map< int , int > mp; int max_freq = 0, max_element = -1; for ( auto x : arr) { mp[x]++; // Getting max_element if (mp[x] > max_freq) { max_freq = mp[x]; max_element = x; } } for ( int i = 0; i < N && K > 0; i++) { // If the element is not equal // to the max_element if (arr[i] != max_element) { // Decrease its frequency from the map mp[arr[i]] -= 1; // Assign the max frequency element arr[i] = max_element; // Increase its frequency in the map mp[arr[i]] += 1; // Decrease the operation by 1 K -= 1; } } // Return the modified array return arr; } // Driver Function int main() { int N = 7; vector< int > arr = { 1, 2, 1, 5, 1, 6, 7 }; int K = 3; // Function call vector< int > res = maximizeTheElements(N, arr, K); for ( auto x : res) { cout << x << " " ; } cout << endl; return 0; } |
Java
// Java code for the above discussed approach import java.util.*; class GFG { // Function to maximizeTheElements static int [] maximizeTheElements( int N, int arr[], int K) { // Map to store the frequency // of the elements HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); int max_freq = 0 , max_element = - 1 ; for ( int i = 0 ; i < arr.length; i++){ if (m.containsKey(arr[i])){ m.put(arr[i], m.get(arr[i]) + 1 ); } else { m.put(arr[i], 1 ); } // Getting max_element if (m.get(arr[i]) > max_freq) { max_freq = m.get(arr[i]); max_element = arr[i]; } } for ( int i = 0 ; i < N; i++) { if (K <= 0 ) break ; // If the element is not equal // to the max_element if (arr[i] != max_element) { // Decrease its frequency from the map m.put(arr[i], m.get(arr[i]) - 1 ); // Assign the max frequency element arr[i] = max_element; // Increase its frequency in the map m.put(arr[i], m.get(arr[i]) + 1 ); // Decrease the operation by 1 K -= 1 ; } } // Return the modified array return arr; } // Driver Function public static void main (String[] args) { int N = 7 ; int arr[] = { 1 , 2 , 1 , 5 , 1 , 6 , 7 }; int K = 3 ; // Function call int res[] = maximizeTheElements(N, arr, K); for ( int x : res) { System.out.print(x + " " ); } } } // This code is contributed by hrithikgarg03188. |
Python3
# Python3 code for the above discussed approach # Function to maximizeTheElements def maximizeTheElements(N, arr, K): # Map to store the frequency # of the elements mp = {} max_freq, max_element = 0 , - 1 for x in arr: mp[x] = mp[x] + 1 if x in mp else 1 # Getting max_element if (mp[x] > max_freq): max_freq = mp[x] max_element = x for i in range ( 0 , N): if K < = 0 : break # If the element is not equal # to the max_element if (arr[i] ! = max_element): # Decrease its frequency from the map mp[arr[i]] - = 1 # Assign the max frequency element arr[i] = max_element # Increase its frequency in the map mp[arr[i]] + = 1 # Decrease the operation by 1 K - = 1 # Return the modified array return arr # Driver Function if __name__ = = "__main__" : N = 7 arr = [ 1 , 2 , 1 , 5 , 1 , 6 , 7 ] K = 3 # Function call res = maximizeTheElements(N, arr, K) for x in res: print (x, end = " " ) # This code is contributed by rakeshsahni |
C#
// C# code for the above discussed approach using System; using System.Collections.Generic; public class GFG { // Function to maximizeTheElements static int [] maximizeTheElements( int N, int [] arr, int K) { // Map to store the frequency // of the elements var m = new Dictionary< int , int >(); int max_freq = 0, max_element = -1; for ( int i = 0; i < arr.Length; i++) { if (m.ContainsKey(arr[i])) { m[arr[i]] += 1; } else { m[arr[i]] = 1; } // Getting max_element if (m[arr[i]] > max_freq) { max_freq = m[arr[i]]; max_element = arr[i]; } } for ( int i = 0; i < N; i++) { if (K <= 0) break ; // If the element is not equal // to the max_element if (arr[i] != max_element) { // Decrease its frequency from the map m[arr[i]] -= 1; // Assign the max frequency element arr[i] = max_element; // Increase its frequency in the map m[arr[i]] += 1; // Decrease the operation by 1 K -= 1; } } // Return the modified array return arr; } // Driver Code public static void Main( string [] args) { int N = 7; int [] arr = { 1, 2, 1, 5, 1, 6, 7 }; int K = 3; // Function call int [] res = maximizeTheElements(N, arr, K); for ( int i = 0; i < res.Length; i++) { Console.Write(res[i] + " " ); } } } // This code is contributed by phasing17 |
Javascript
<script> // JavaScript code for the above approach // Function to maximizeTheElements function maximizeTheElements( N, arr, K) { // Map to store the frequency // of the elements let mp = new Map(); let max_freq = 0, max_element = -1; for (let x of arr) { if (!mp.has(x)) { mp.set(x, 1) } else { mp.set(x, mp.get(x) + 1) } // Getting max_element if (mp.get(x) > max_freq) { max_freq = mp.get(x); max_element = x; } } for (let i = 0; i < N && K > 0; i++) { // If the element is not equal // to the max_element if (arr[i] != max_element) { // Decrease its frequency from the map mp.set(arr[i], mp.get(arr[i]) - 1) // Assign the max frequency element arr[i] = max_element; // Increase its frequency in the map mp.set(arr[i], mp.get(arr[i]) + 1) // Decrease the operation by 1 K -= 1; } } // Return the modified array return arr; } // Driver Function let N = 7; let arr = [1, 2, 1, 5, 1, 6, 7]; let K = 3; // Function call let res = maximizeTheElements(N, arr, K); for (let x of res) { document.write(x + " " ); } // This code is contributed by Potta Lokesh </script> |
Output
1 1 1 1 1 1 7
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...