📌  相关文章
📜  使数组中的所有元素与众不同所需的最少操作

📅  最后修改于: 2021-05-06 22:13:09             🧑  作者: Mango

给定一个由N个整数组成的数组。如果一个数字多次出现,请从数组中选择任意数字y,并将数组中的x替换为x + y,以使x + y不在数组中。任务是找到使数组与众不同的最少操作数。

例子:

方法:如果一个数字出现不止一次,则所有重复元素的(occurrences-1)总和将成为答案。这背后的主要逻辑是,如果x被x + y替换,其中y是数组中的最大元素,那么x被x + y替换,x + y是数组中的最大元素。使用映射来存储数组编号的频率。在地图中遍历,如果元素的频率大于1,则将其减去1,即可将其添加到计数中。

下面是上述方法的实现:

C++
// C++ program to find Minimum number
// of  changes to make array distinct
#include 
using namespace std;
  
// Fnction that returns minimum number of changes
int minimumOperations(int a[], int n)
{
  
    // Hash-table to store frequency
    unordered_map mp;
  
    // Increase the frequency of elements
    for (int i = 0; i < n; i++)
        mp[a[i]] += 1;
  
    int count = 0;
  
    // Traverse in the map to sum up the (occurrences-1)
    // of duplicate elements
    for (auto it = mp.begin(); it != mp.end(); it++) {
        if ((*it).second > 1)
            count += (*it).second-1;
    }
    return count;
}
  
// Driver Code
int main()
{
    int a[] = { 2, 1, 2, 3, 3, 4, 3 };
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << minimumOperations(a, n);
    return 0;
}


Java
// Java program to find Minimum number 
// of changes to make array distinct 
import java.util.*;
  
class geeks
{
  
    // Function that returns minimum number of changes
    public static int minimumOperations(int[] a, int n)
    {
  
        // Hash-table to store frequency
        HashMap mp = new HashMap<>();
  
        // Increase the frequency of elements
        for (int i = 0; i < n; i++)
        {
            if (mp.get(a[i]) != null)
            {
                int x = mp.get(a[i]);
                mp.put(a[i], ++x);
            }
            else
                mp.put(a[i], 1);
        }
  
        int count = 0;
  
        // Traverse in the map to sum up the (occurrences-1)
        // of duplicate elements
        for (HashMap.Entry entry : mp.entrySet())
        {
            if (entry.getValue() > 1)
            {
                count += (entry.getValue() - 1);
            }
        }
  
        return count;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int[] a = { 2, 1, 2, 3, 3, 4, 3 };
        int n = a.length;
  
        System.out.println(minimumOperations(a, n));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to find Minimum number
# of changes to make array distinct
  
# Function that returns minimum 
# number of changes
def minimumOperations(a, n):
  
    # Hash-table to store frequency
    mp = dict()
  
    # Increase the frequency of elements
    for i in range(n):
        if a[i] in mp.keys():
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
  
    count = 0
  
    # Traverse in the map to sum up the 
    # (occurrences-1)of duplicate elements
    for it in mp:
        if (mp[it] > 1):
            count += mp[it] - 1
      
    return count
  
# Driver Code
a = [2, 1, 2, 3, 3, 4, 3 ]
n = len(a)
  
print(minimumOperations(a, n))
      
# This code is contributed
# by Mohit Kumar


C#
// C# program to find Minimum number 
// of changes to make array distinct 
using System;
using System.Collections.Generic;
  
class geeks
{
  
    // Function that returns minimum number of changes
    public static int minimumOperations(int[] a, int n)
    {
  
        // Hash-table to store frequency
        Dictionary mp = new Dictionary();
        // Increase the frequency of elements
        for (int i = 0 ; i < n; i++)
        {
            if(mp.ContainsKey(a[i]))
            {
                var val = mp[a[i]];
                mp.Remove(a[i]);
                mp.Add(a[i], val + 1); 
            }
            else
            {
                mp.Add(a[i], 1);
            }
        }
  
        int count = 0;
  
        // Traverse in the map to sum up the (occurrences-1)
        // of duplicate elements
        foreach(KeyValuePair entry in mp)
        {
            if (entry.Value > 1)
            {
                count += (entry.Value - 1);
            }
        }
  
        return count;
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        int[] a = { 2, 1, 2, 3, 3, 4, 3 };
        int n = a.Length;
  
        Console.WriteLine(minimumOperations(a, n));
    }
}
  
/* This code is contributed by PrinciRaj1992 */


输出:
3

时间复杂度: O(N)