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

📅  最后修改于: 2021-10-26 03:05:06             🧑  作者: 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))


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程