Minimum replacements to make elements of a ternary array same
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++
#include <bits/stdc++.h>
using namespace std;
int minReplacements( int arr[], int n)
{
int freq[3] = { 0 };
for ( int i = 0; i < n; i++)
freq[arr[i] - 1]++;
int max_freq = *max_element(freq, freq + 3);
return (n - max_freq);
}
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
import java .io.*;
import java .util.*;
class GFG
{
static int minReplacements( int []arr,
int n)
{
int []freq = new int [ 3 ];
for ( int i = 0 ; i < n; i++)
freq[arr[i] - 1 ]++;
Arrays.sort(freq);
int max_freq = freq[ 2 ];
return (n - max_freq);
}
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));
}
}
|
Python 3
def minReplacements(arr, n):
freq = [ 0 ] * 3
for i in range (n):
freq[arr[i] - 1 ] + = 1
freq.sort()
max_freq = freq[ 2 ]
return (n - max_freq)
if __name__ = = "__main__" :
arr = [ 1 , 3 , 2 , 2 ,
2 , 1 , 1 , 2 , 3 ]
n = len (arr)
print ( minReplacements(arr, n) )
|
C#
using System;
using System.Linq;
public class GFG
{
static int minReplacements( int []arr, int n)
{
int []freq = new int [3];
for ( int i = 0; i < n; i++)
freq[arr[i] - 1]++;
int max_freq = freq.Max();
return (n - max_freq);
}
static public void Main ()
{
int []arr = {1, 3, 2, 2, 2, 1, 1, 2, 3};
int n = arr.Length;
Console.WriteLine(minReplacements(arr, n));
}
}
|
Javascript
<script>
function minReplacements(arr, n)
{
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];
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)
Last Updated :
22 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...