Minimum replacements to make elements of a ternary array same
Last Updated :
22 Jul, 2022
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.
Implementation:
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.
JavaScript
<script>
// Javascript program minimum
// number of replacements
// needed to be performed
// to make all the numbers
// in the given array equal
// Function for
// minimum replacements
function minReplacements(arr, n)
{
// Find the most
// frequent element
let freq = new Array(3);
freq.fill(0);
for (let i = 0; i < n; i++)
freq[arr[i] - 1]++;
freq.sort();
let max_freq = freq[2];
// Returning count of
// replacing other elements
// with the most frequent
return (n - max_freq);
}
let arr = [1, 3, 2, 2, 2, 1, 1, 2, 3];
let n = arr.length;
document.write(minReplacements(arr, n));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum delete operations to make all elements of array same Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.Examples: Input: arr[] = {4, 3, 4, 4, 2, 4} Output: 2 After deleting 2 and 3 from array, arr
10 min read
Minimum replacements to make adjacent characters unequal in a ternary string Given a string of '0', '1' and '2'. The task is to find the minimum number of replacements such that the adjacent characters are not equal. Examples: Input: s = "201220211" Output: 2 Resultant string after changes is 201210210 Input: s = "0120102" Output: 0 Approach: The following problem can be sol
7 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Minimum operations to make all elements equal using the second array Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 <= i <= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] =
7 min read
Minimize steps to make Array elements 0 by reducing same A[i] - X from Subarray Given an array A[] of size N, the task is to find the minimum number of operations required to make all the elements of the array zero. In one step, the following operations are done on a subarray of the given array: Any integer X is chosenIf an element is greater than X (A[i] > X), the array ele
6 min read
Minimum replacements to make adjacent characters unequal in a ternary string | Set-2 Given a string of â0â, â1â, and â2â. The task is to find the minimum number of replacements such that the adjacent characters are not equal. Examples: Input: s = â201220211â Output: 2 Resultant string after changes is 201210210 Input: s = â0120102â Output: 0 Approach: The problem has been solved usi
15+ min read