📌  相关文章
📜  使数组中的所有元素相等所需的最小更改

📅  最后修改于: 2021-04-22 04:18:17             🧑  作者: Mango

给定长度为N的数组,任务是找到使数组中的所有元素相等所需的最小操作。
操作如下:

  • 将数组一个元素的值替换为其相邻元素之一。

例子:

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

方法:
让我们假设在执行所需的最小更改后,数组的所有元素都将变为X。假定我们仅被允许将数组元素的值替换为其相邻元素,因此X应该是元素之一的数组。
同样,由于我们需要将更改最小化,因此X应该是数组中出现的最大元素。一旦找到X的值,就需要对每个非相等元素(不是X的元素)进行一次更改,以使数组的所有元素等于X。

  • 查找数组中出现的最大元素的计数。
  • 使数组的所有元素相等所需的最小更改为
    所有元素的数量–最大出现元素的数量

下面是上述方法的实现:

CPP
// C++ program to find minimum
// changes required to make
// all elements of the array equal
#include 
using namespace std;
 
// Function to count
// of minimum changes
// required to make all
// elements equal
int minChanges(int arr[], int n)
{
 
    unordered_map 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 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 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 mp
            = new Dictionary();
 
        // 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 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))


输出:
2