Minimum operation to make all elements equal in array
Last Updated :
17 Apr, 2023
Given an array with n positive integers. We need to find the minimum number of operations to make all elements equal. We can perform addition, multiplication, subtraction, or division with any part on an array element.
Examples:
Input : arr[] = {1, 2, 3, 4}
Output : 3
Since all elements are different,
we need to perform at least three
operations to make them same. For
example, we can make them all 1
by doing three subtractions. Or make
them all 3 by doing three additions.
Input : arr[] = {1, 1, 1, 1}
Output : 0
To make all elements equal you can select a target value and then you can make all elements equal to that. Now, for converting a single element to target value you can perform a single operation only once. In this manner, you can achieve your task in a maximum of n operations but you have to minimize this number of operations and for this, your selection of target is very important because if you select a target whose frequency in array is x then you have to perform only n-x more operations as you have already x elements equal to your target value. So finally, our task is reduced to finding the element with maximum frequency. This can be achieved by different means such as the iterative method in O(n^2), sorting in O(nlogn), and hashing in O(n) time complexity.
Step-by-step approach:
- Create an empty hash table to store the frequency of each element in the array.
- Traverse the array and insert each element into the hash table. If an element is already present in the hash table, increment its frequency.
- Find the maximum frequency of any element in the hash table.
- The minimum number of operations required to make all elements equal is equal to the difference between the total number of elements in the array and the maximum frequency of any element in the hash table
Pseudocode:
minOperations(arr, n)
// Step 1
hashTable = {}
// Step 2
for i = 0 to n-1 do
if arr[i] is in hashTable then
hashTable[arr[i]] = hashTable[arr[i]] + 1
else
hashTable[arr[i]] = 1
end if
end for
// Step 3
maxCount = 0
for key in hashTable do
if hashTable[key] > maxCount then
maxCount = hashTable[key]
end if
end for
// Step 4
return n - maxCount
end function
Implementation:
C++
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
int minOperation( int arr[], int n) {
unordered_map< int , int > hash;
for ( int i=0; i<n; i++) {
if (hash.find(arr[i]) != hash.end()) {
hash[arr[i]]++;
} else {
hash[arr[i]] = 1;
}
}
int max_count = 0;
for ( auto it : hash) {
max_count = max(max_count, it.second);
}
return (n - max_count);
}
int main() {
int arr[] = {1, 5, 2, 1, 3, 2, 1};
int n = sizeof (arr) / sizeof (arr[0]);
cout << minOperation(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minOperation ( int arr[], int n)
{
HashMap<Integer, Integer> hash = new HashMap<Integer,
Integer>();
for ( int i= 0 ; i<n; i++)
if (hash.containsKey(arr[i]))
hash.put(arr[i], hash.get(arr[i])+ 1 );
else hash.put(arr[i], 1 );
int max_count = 0 ;
Set<Integer> s = hash.keySet();
for ( int i : s)
if (max_count < hash.get(i))
max_count = hash.get(i);
return (n - max_count);
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 };
int n = arr.length;
System.out.print(minOperation(arr, n));
}
}
|
Python3
def minOperation(arr, n):
mp = {}
max_freq = 0
for i in arr:
mp[i] = mp.get(i, 0 ) + 1
for i in mp:
if mp[i] > max_freq:
max_freq = mp[i]
if max_freq = = 1 :
return n - 1
return (n - max_freq)
if __name__ = = "__main__" :
arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ]
n = len (arr)
print (minOperation(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int minOperation ( 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 max_count = 0;
HashSet< int > s = new HashSet< int >(m.Keys);
foreach ( int i in s)
if (max_count < m[i])
max_count = m[i];
return (n - max_count);
}
public static void Main(String[] args)
{
int []arr = {1, 5, 2, 1, 3, 2, 1};
int n = arr.Length;
Console.Write(minOperation(arr, n));
}
}
|
Javascript
<script>
function minOperation(arr, n) {
let hash = new Map();
for (let i = 0; i < n; i++)
if (hash.has(arr[i]))
hash.set(arr[i], hash.get(arr[i]) + 1);
else hash.set(arr[i], 1);
let max_count = 0;
let s = hash.keys();
for (let i of s)
if (max_count < hash.get(i))
max_count = hash.get(i);
return (n - max_count);
}
let arr = [1, 5, 2, 1, 3, 2, 1];
let n = arr.length;
document.write(minOperation(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Ritik Malarya
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...