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
#include <bits/stdc++.h>
using namespace std;
int minChanges( int arr[], int n)
{
unordered_map< int , int > umap;
for ( int i = 0; i < n; i++) {
umap[arr[i]]++;
}
int maxFreq = 0;
for ( auto p : umap) {
maxFreq = max(maxFreq, p.second);
}
return n - maxFreq;
}
int main()
{
int arr[] = { 2, 3, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minChanges(arr, n) << '\n' ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minChanges( int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
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 ;
for (Map.Entry<Integer, Integer> entry :
mp.entrySet()) {
maxElem = Math.max(maxElem, entry.getValue());
}
return n - maxElem;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 3 , 4 };
int n = arr.length;
System.out.println(minChanges(arr, n));
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int minChanges( int [] arr, int n)
{
Dictionary< int , int > mp
= new Dictionary< int , int >();
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;
foreach (KeyValuePair< int , int > entry in mp)
{
maxElem = Math.Max(maxElem, entry.Value);
}
return n - maxElem;
}
public static void Main( string [] args)
{
int [] arr = { 2, 3, 3, 4 };
int n = arr.Length;
Console.WriteLine(minChanges(arr, n));
}
}
|
Python3
def minChanges(arr, n):
mp = dict ()
for i in range (n):
if arr[i] in mp.keys():
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
maxElem = 0
for x in mp:
maxElem = max (maxElem, mp[x])
return n - maxElem
arr = [ 2 , 3 , 3 , 4 ]
n = len (arr)
print (minChanges(arr, n))
|
Javascript
<script>
function minChanges( arr, n)
{
var umap = new 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;
umap.forEach((values,keys)=>{
maxFreq = Math.max(maxFreq, values);
});
return n - maxFreq;
}
var arr = [ 2, 3, 3, 4 ];
var n = arr.length;
document.write( minChanges(arr, n) + '<br>' );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Feb, 2022
Like Article
Save Article