Minimize the maximum frequency of Array elements by replacing them only once
Last Updated :
21 Dec, 2022
Given an array A[] of length N, the task is to minimize the maximum frequency of any array element by performing the following operation only once:
- Choose any random set (say S) of indices i (0 ? i ? N-1) of the array such that every element of S is the same and
- Replace every element of that index with any other number.
Examples:
Input: A[] = {1 ,4 ,4 ,1 ,4 , 4}
Output: 2
Explanation: Choose index i = {1, 2} and perform the operation
{ 1, 4, 4, 1, 4, 4 }? { 1, 5, 5, 1, 4, 4 }.
The highest frequency is of element 4, 5 and 1 which is 2.
This is the minimum possible value of highest frequency of an element.
Input: A[] = {5 ,5 ,5 ,5 ,5 }
Output: 3
Approach: The problem can be solved based on the following observation:
- Let N be the element with the maximum frequency in A[] and M be the element with the second maximum frequency in A[]. It is possible that M does not exist.
- If M exist then maximum frequency = freqN.
- Indices such that A[i] = N, should be chosen because the final value of freqN should be minimized and it is always better to replace with number(num) such that num ? M. Only a maximum of ? freqN/2? elements should be replaced because if we replace more, then num becomes the new element with the maximum frequency
- Hence, it is optimal to replace ? freqN/2? occurrences of N to some num ? M.Now, freqN - ? freqN/2? is the final frequency of N and freqM is the final frequency of M.
- The maximum among these two i.e. Max( freqN - ? freqN/2?, freqM ) has to be the answer because the frequency of num = ?freqN/2? ?( freqN - ? freqN/2?) = frequency of N .
- If M does not exist it means freqM = 0 . So , the answer is freqN - ? freqN/2?.
Follow the below steps to implement the above idea:
- Find the frequency of the most frequent and the second most frequent element.
- Compare those frequencies.
- Based on the frequency, follow the above conditions to find the minimized value of maximum frequency.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to minimize frequency of
// most frequent element
int minFreq(int arr[], int n)
{
int b[n];
for (int i = 0; i < n; i++) {
b[i] = 0;
}
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
if (mp.count(arr[i]) < 0) {
mp[arr[i]] = 1;
}
else {
mp[arr[i]]++;
}
}
for (int i = 0; i < n; i++) {
if (mp[arr[i]] != -1) {
// Count frequency of each element
b[i] = mp[arr[i]];
mp[arr[i]] = -1;
}
}
// Last index value is maximum
// frequency of any element
sort(b, b + n);
if (b[n - 1] % 2 == 0) {
b[n - 1] = b[n - 1] / 2;
}
else {
b[n - 1] = (b[n - 1] + 1) / 2;
}
// Last index value is possible
// minimum frequency of
// most frequent element
sort(b, b + n);
return b[n - 1];
}
int main()
{
int A[] = { 1, 4, 4, 1, 4, 4 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << (minFreq(A, N));
return 0;
}
// This code is contributed by akashish__
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG {
// Function to minimize frequency of
// most frequent element
public static int minFreq(int arr[], int n)
{
int[] b = new int[n];
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < n; i++) {
mp.put(arr[i], mp.get(arr[i]) == null
? 1
: mp.get(arr[i]) + 1);
}
for (int i = 0; i < n; i++) {
if (mp.get(arr[i]) != -1) {
// Count frequency of each element
b[i] = mp.get(arr[i]);
mp.put(arr[i], -1);
}
}
// Last index value is maximum
// frequency of any element
Arrays.sort(b);
if (b[n - 1] % 2 == 0) {
b[n - 1] = b[n - 1] / 2;
}
else {
b[n - 1] = (b[n - 1] + 1) / 2;
}
// Last index value is possible
// minimum frequency of
// most frequent element
Arrays.sort(b);
return b[n - 1];
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 1, 4, 4, 1, 4, 4 };
int N = A.length;
// Function call
System.out.println(minFreq(A, N));
}
}
Python3
# Python3 code to implement the above approach
# Function to minimize frequency of
# most frequent element
def minFreq(arr, n) :
b = [0] * n;
mp = dict.fromkeys(arr, 0);
for i in range(n) :
if arr[i] in mp :
mp[arr[i]] +=1;
else :
mp[arr[i]] = 1;
for i in range(n) :
if (mp[arr[i]] != -1) :
# Count frequency of each element
b[i] = mp[arr[i]];
mp[arr[i]] = -1;
# Last index value is maximum
# frequency of any element
b.sort()
if (b[n - 1] % 2 == 0) :
b[n - 1] = b[n - 1] // 2;
else :
b[n - 1] = (b[n - 1] + 1) // 2;
# Last index value is possible
# minimum frequency of
# most frequent element
b.sort();
return b[n - 1];
if __name__ == "__main__" :
A = [ 1, 4, 4, 1, 4, 4 ];
N = len(A);
# Function call
print(minFreq(A, N));
# This code is contributed by AnkThon
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
class GFG {
// Function to minimize frequency of
// most frequent element
public static int minFreq(int[] arr, int n)
{
int[] b = new int[n];
Dictionary<int, int> mp
= new Dictionary<int, int>();
for (int i = 0; i < n; i++) {
if (mp.ContainsKey(arr[i]))
mp[arr[i]]++;
else
mp.Add(arr[i], 1);
}
for (int i = 0; i < n; i++) {
if (mp[arr[i]] != -1) {
// Count frequency of each element
b[i] = mp[arr[i]];
mp[arr[i]] = -1;
}
}
// Last index value is maximum
// frequency of any element
Array.Sort(b);
if (b[n - 1] % 2 == 0) {
b[n - 1] = b[n - 1] / 2;
}
else {
b[n - 1] = (b[n - 1] + 1) / 2;
}
// Last index value is possible
// minimum frequency of
// most frequent element
Array.Sort(b);
return b[n - 1];
}
// Driver Code
public static void Main(String[] args)
{
int[] A = { 1, 4, 4, 1, 4, 4 };
int N = A.Length;
// Function call
Console.WriteLine(minFreq(A, N));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript code to implement the approach
// Function to minimize frequency of
// most frequent element
function minFreq(arr, n) {
let b = new Array(n).fill(0);
let mp = new Map();
for (let i = 0; i < n; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
} else {
mp.set(arr[i], 1)
}
}
for (let i = 0; i < n; i++) {
if (mp.get(arr[i]) != -1) {
// Count frequency of each element
b[i] = mp.get(arr[i]);
mp.set(arr[i], -1);
}
}
// Last index value is maximum
// frequency of any element
b.sort((a, b) => a - b);
if (b[n - 1] % 2 == 0) {
b[n - 1] = Math.floor(b[n - 1] / 2);
}
else {
b[n - 1] = Math.floor((b[n - 1] + 1) / 2);
}
// Last index value is possible
// minimum frequency of
// most frequent element
b.sort((a, b) => a - b);
return b[n - 1];
}
// Driver Code
let A = [1, 4, 4, 1, 4, 4];
let N = A.length;
// Function call
document.write(minFreq(A, N));
// This code is contributed by saurabh_jaiswal.
</script>
Time Complexity: O(N * logN) //the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(N) // an extra array is used and in the worst case all elements will be stored inside it the hence algorithm takes up linear space
Similar Reads
Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e
8 min read
Maximize the minimum element of Array by reducing elements one by one Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations. Examples: Input:
6 min read
Maximum Frequency by replacing with elements in a given range Given a 0-indexed array arr[] of N positive integers and two integers K and X. Find the maximum frequency of any element(not necessary to be present in the array) you can make after performing the below operation at most K times- Choose an index i and replace arr[i] with any integer from the range [
9 min read
Replace every elements in the array by its frequency in the array Given an array of integers, replace every element by its frequency in the array. Examples: Input : arr[] = { 1, 2, 5, 2, 2, 5 } Output : 1 3 2 3 3 2 Input : arr[] = { 4 5 4 5 6 6 6 } Output : 2 2 2 2 3 3 3 Approach: Take a hash map, which will store the frequency of all the elements in the array.Now
5 min read
Make the array elements equal by performing given operations minimum number of times Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
7 min read
Find the only non-repeating element in a given array Given an array A[] consisting of N (1 ? N ? 105) positive integers, the task is to find the only array element with a single occurrence. Note: It is guaranteed that only one such element exists in the array. Examples: Input: A[] = {1, 1, 2, 3, 3}Output: 2Explanation: Distinct array elements are {1,
10 min read
Maximize the cost of reducing array elements Given an array arr[] of N positive integers. We can choose any one index(say K) of the array and reduce all the elements of the array from index 0 to K - 1 by 1. The cost of this operation is K. If at any index(say idx) element is reduced to 0 then we can't perform this operation in the range [idx,
6 min read
Modify array by replacing every array element with minimum possible value of arr[j] + |j - i| Given an array arr[] of size N, the task is to find a value for each index such that the value at index i is arr[j] + |j - i| where 1 ? j ? N, the task is to find the minimum value for each index from 1 to N. Example: Input: N = 5, arr[] = {1, 4, 2, 5, 3}Output: {1, 2, 2, 3, 3}Explanation: arr[0] =
11 min read
Find the minimum number of operations required to make all array elements equal Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Find the array element having maximum frequency of the digit K Given an array arr[] of size N and an integer K, the task is to find an array element that contains the digit K a maximum number of times. If more than one solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = {3, 77, 343, 456}, K = 3 Output: 343 Explanation: Fre
15 min read