Minimum changes required to make all element in an array equal
Last Updated :
20 Feb, 2022
Given an array of length N, the task is to find minimum operation required to make all elements in the array equal.
Operation is as follows:
- Replace the value of one element of the array by one of its adjacent elements.
Examples:
Input: N = 4, arr[] = {2, 3, 3, 4}
Output: 2
Explanation:
Replace 2 and 4 by 3
Input: N = 4, arr[] = { 1, 2, 3, 4}
Output: 3
Approach:
Let us assume that after performing the required minimum changes all elements of the array will become X. It is given that we are only allowed to replace the value of an element of the array with its adjacent element, So X should be one of the elements of the array.
Also, as we need to make changes as minimum as possible X should be the maximum occurring element of the array. Once we find the value of X, we need only one change per non-equal element (elements which are not X) to make all elements of the array equal to X.
- Find the count of the maximum occurring element of the array.
- Minimum changes required to make all elements of the array equal is
count of all elements - count of maximum occurring element
Below is the implementation of the above approach:
CPP
// C++ program to find minimum
// changes required to make
// all elements of the array equal
#include <bits/stdc++.h>
using namespace std;
// Function to count
// of minimum changes
// required to make all
// elements equal
int minChanges(int arr[], int n)
{
unordered_map<int, int> umap;
// Store the count of
// each element as key
// value pair in unordered map
for (int i = 0; i < n; i++) {
umap[arr[i]]++;
}
int maxFreq = 0;
// Find the count of
// maximum occurring element
for (auto p : umap) {
maxFreq = max(maxFreq, p.second);
}
// Return count of all
// element minus count
// of maximum occurring element
return n - maxFreq;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << minChanges(arr, n) << '\n';
return 0;
}
Java
// Java program to find minimum
// changes required to make
// all elements of the array equal
import java.util.*;
class GFG {
// Function to count of minimum changes
// required to make all elements equal
static int minChanges(int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
// Store the count of each element
// as key value pair in map
for (int i = 0; i < n; i++) {
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else {
mp.put(arr[i], 1);
}
}
int maxElem = 0;
// Traverse through map and
// find the maximum occurring element
for (Map.Entry<Integer, Integer> entry :
mp.entrySet()) {
maxElem = Math.max(maxElem, entry.getValue());
}
// Return count of all element minus
// count of maximum occurring element
return n - maxElem;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 3, 3, 4 };
int n = arr.length;
// Function call
System.out.println(minChanges(arr, n));
}
}
C#
// C# program to find minimum
// changes required to make
// all elements of the array equal
using System;
using System.Collections.Generic;
class GFG {
// Function to count of minimum changes
// required to make all elements equal
static int minChanges(int[] arr, int n)
{
Dictionary<int, int> mp
= new Dictionary<int, int>();
// Store the count of each element
// as key-value pair in Dictionary
for (int i = 0; i < n; i++) {
if (mp.ContainsKey(arr[i])) {
var val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else {
mp.Add(arr[i], 1);
}
}
int maxElem = 0;
// Traverse through the Dictionary and
// find the maximum occurring element
foreach(KeyValuePair<int, int> entry in mp)
{
maxElem = Math.Max(maxElem, entry.Value);
}
// Return count of all element minus
// count of maximum occurring element
return n - maxElem;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 2, 3, 3, 4 };
int n = arr.Length;
// Function call
Console.WriteLine(minChanges(arr, n));
}
}
Python3
# Python3 program to find minimum
# changes required to make
# all elements of the array equal
# Function to count of minimum changes
# required to make all elements equal
def minChanges(arr, n):
mp = dict()
# Store the count of each element
# as key-value pair in Dictionary
for i in range(n):
if arr[i] in mp.keys():
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
maxElem = 0
# Traverse through the Dictionary and
# find the maximum occurring element
for x in mp:
maxElem = max(maxElem, mp[x])
# Return count of all element minus
# count of maximum occurring element
return n - maxElem
# Driver code
arr = [2, 3, 3, 4]
n = len(arr)
# Function call
print(minChanges(arr, n))
JavaScript
<script>
// Javascript program to find minimum
// changes required to make
// all elements of the array equal
// Function to count
// of minimum changes
// required to make all
// elements equal
function minChanges( arr, n)
{
var umap = new Map();
// Store the count of
// each element as key
// value pair in unordered map
for (var i = 0; i < n; i++) {
if(umap.has(arr[i]))
{
umap.set(arr[i], umap.get(arr[i])+1);
}
else
{
umap.set(arr[i], 1);
}
}
var maxFreq = 0;
// Find the count of
// maximum occurring element
umap.forEach((values,keys)=>{
maxFreq = Math.max(maxFreq, values);
});
// Return count of all
// element minus count
// of maximum occurring element
return n - maxFreq;
}
// Driver code
var arr = [ 2, 3, 3, 4 ];
var n = arr.length;
// Function call
document.write( minChanges(arr, n) + '<br>');
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Minimum cost to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to make all values of this array equal to some integer value with minimum cost after performing below operations any number of times (possibly zero). Reduce the array element by 2 or increase it by 2 with zero cost.Reduce the array
5 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
Make all array elements equal with minimum cost Given an array of size n, the task is to make the value of all elements equal with minimum cost. The cost of changing a value from x to y is abs(x - y).Examples : Input: arr[] = [1, 100, 101]Output: 100Explanation: We can change all its values to 100 with minimum cost,|1 - 100| + |100 - 100| + |101
15 min read
Minimize cost of insertions and deletions required to make all array elements equal Given a sorted array arr[] of size N (1 ? N ? 105) and two integers A and B, the task is to calculate the minimum cost required to make all array elements equal by increments or decrements. The cost of each increment and decrement are A and B respectively. Examples: Input: arr[] = { 2, 5, 6, 9, 10,
11 min read
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read