Find the minimum number of operations required to make all array elements equal
Last Updated :
28 Sep, 2023
Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times:
- Choose a pair of indices (i, j) such that |i – j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] – arr[j]|
- Choose a pair of indices (i, j) such that |i – j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] – |arr[i] – arr[j]|
Examples:
Input: arr[] = { 2, 4, 6 }
Output: 2
Applying the 2nd type of operation on the given array gives {2, 2, 6}.
Now, applying 2nd type of operation again on the modified array gives {2, 2, 2}.
Input: arr[] = { 1, 1, 1}
Output: 0
All array elements are already equal.
Approach: Let’s find the most frequent element in the array (using map to store the frequencies of all the elements). Let this element be x. If we observe the operations more carefully, we can see that the part of these operations mean set element p to element q. If p < q then first operation needs to be performed, otherwise second.
Now, consider the number of operations in the optimal answer. It is obvious that we need at least n – freq(x) operations to equalize all the elements. And it is also obvious that we can always do it with n – freq(x) such operations which is the minimum number of operations required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
int maxFreq = INT_MIN;
for ( auto x : mp)
maxFreq = max(maxFreq, x.second);
return (n - maxFreq);
}
int main()
{
int arr[] = { 2, 4, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minOperations(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minOperations( int arr[], int n)
{
HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
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 maxFreq = Integer.MIN_VALUE;
maxFreq = Collections.max(mp.entrySet(),
Comparator.comparingInt(Map.Entry::getKey)).getValue();
return (n - maxFreq);
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 6 };
int n = arr.length;
System.out.println(minOperations(arr, n));
}
}
|
Python3
import sys
def minOperations(arr, n) :
mp = dict .fromkeys(arr, 0 );
for i in range (n) :
mp[arr[i]] + = 1 ;
maxFreq = - (sys.maxsize - 1 );
for key in mp :
maxFreq = max (maxFreq, mp[key]);
return (n - maxFreq);
if __name__ = = "__main__" :
arr = [ 2 , 4 , 6 ];
n = len (arr) ;
print (minOperations(arr, n));
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int minOperations( int []arr, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (m.ContainsKey(arr[i]))
{
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else
{
m.Add(arr[i], 1);
}
}
int maxFreq = int .MinValue;
maxFreq = m.Values.Max();
return (n - maxFreq);
}
public static void Main(String[] args)
{
int []arr = {2, 4, 6};
int n = arr.Length;
Console.WriteLine(minOperations(arr, n));
}
}
|
Javascript
<script>
function minOperations(arr, n) {
let mp = new Map();
for (let i = 0; i < n; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
} else {
mp.set(arr[i], 1)
}
}
let maxFreq = Number.MIN_SAFE_INTEGER;
for (let x of mp)
maxFreq = Math.max(maxFreq, x[1]);
return (n - maxFreq);
}
let arr = [2, 4, 6];
let n = arr.length;
document.write(minOperations(arr, n));
</script>
|
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(N), where N represents the size of the given array.
Share your thoughts in the comments
Please Login to comment...