Given a ternary array (every element has one the three possible values 1, 2 and 3). Our task is to replace the minimum number of numbers in it so that all the numbers in the array are equal to each other.
Examples:
Input : arr[] = 1 3 2 2 2 1 1 2 3 Output : 5 In this example, frequency of 1 is 3, frequency of 2 is 4 and frequency of 3 is 2. As we can see that 2 is having the more frequency than 1 and 3. So, if we replace all the 1's and 3's by 2 then, the resultant array has all the elements equal to each other in minimum replacements. Here, total no. of 1's and 3's is 5 so it takes 5 replacements to replace them by 2. Hence, the output is 5. Input : arr[] = 3 3 2 2 1 3 Output : 3 In this example, 3 has the max frequency. Hence, minimum number of replacements are 3 to replace 1 and 2 by 3. Hence, the output is 3.
The approach is to calculate frequency of each element of the given array. Then, the difference of n(no. of elements) and max_frequency(frequency of the element occurs maximum time in the array) will be minimum number of replacements needed.
C++
// CPP program minimum number of replacements // needed to be performed to make all the numbers // in the given array equal. #include <bits/stdc++.h> using namespace std; int minReplacements(int arr[], int n) { // Find the most frequent element int freq[3] = { 0 }; for (int i = 0; i < n; i++) freq[arr[i] - 1]++; int max_freq = *max_element(freq, freq + 3); // Returning count of replacing other elements // with the most frequent. return (n - max_freq); } // Driver Function int main() { int arr[] = { 1, 3, 2, 2, 2, 1, 1, 2, 3 }; int n = sizeof(arr) / sizeof(arr[0]); cout << minReplacements(arr, n) << endl; return 0; } |
Java
// Java program minimum // number of replacements // needed to be performed // to make all the numbers // in the given array equal import java .io.*; import java .util.*; class GFG { // Function for // minimum replacements static int minReplacements(int []arr, int n) { // Find the most // frequent element int []freq = new int[3]; for (int i = 0; i < n; i++) freq[arr[i] - 1]++; Arrays.sort(freq); int max_freq = freq[2]; // Returning count of // replacing other elements // with the most frequent return (n - max_freq); } // Driver code static public void main (String[] args) { int []arr = {1, 3, 2, 2, 2, 1, 1, 2, 3}; int n = arr.length; System.out.println(minReplacements(arr, n)); } } // This code is contributed // by anuj_67. |
Python 3
# Python 3 program minimum number of # replacements needed to be performed # to make all the numbers in the given # array equal. def minReplacements(arr, n): # Find the most frequent element freq = [0] * 3 for i in range(n): freq[arr[i] - 1] += 1 freq.sort() max_freq = freq[2] # Returning count of replacing other # elements with the most frequent. return (n - max_freq) # Driver Code if __name__ == "__main__": arr = [ 1, 3, 2, 2, 2, 1, 1, 2, 3 ] n = len(arr) print( minReplacements(arr, n) ) # This code is contributed # by ChitraNayal |
C#
// C# program minimum number of // replacements needed to be // performed to make all the // numbers in the given array equal using System; using System.Linq; public class GFG { // Function for minimum replacements static int minReplacements(int []arr, int n) { // Find the most frequent element int []freq = new int[3]; for (int i = 0; i < n; i++) freq[arr[i] - 1]++; int max_freq = freq.Max(); // Returning count of replacing other // elements with the most frequent return (n - max_freq); } // Driver code static public void Main () { int []arr = {1, 3, 2, 2, 2, 1, 1, 2, 3}; int n = arr.Length; Console.WriteLine(minReplacements(arr, n)); } } // This code is contributed by vt_m. |
Output:
5
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.
Recommended Posts:
- Minimize replacements by integers up to K to make the sum of equidistant array elements from the end equal
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Minimize difference between maximum and minimum of Array by at most K replacements
- Minimum increments to make all array elements equal with sum same as the given array after exactly one removal
- Convert string X to an anagram of string Y with minimum replacements
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum replacements required to obtain a K-periodic palindromic string
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum delete operations to make all elements of array same
- Minimum Group Flips to Make Binary Array Elements Same
- Array Queries for multiply, replacements and product
- Queries to replace subarrays by equal length arrays with at most P replacements allowed for any array element
- Largest in array without using conditionals / bitwise / ternary operators
- Minimum swaps of same-indexed elements required to make sum of two given arrays even
- Minimum sum of values subtracted from array elements to make all array elements equal
- Minimum operations to make counts of remainders same in an array
- Find whether it is possible to make array elements same using one external number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

