📌  相关文章
📜  要删除的数组元素的计数以使每对之间的绝对差异相同

📅  最后修改于: 2022-05-13 01:56:07.882000             🧑  作者: Mango

要删除的数组元素的计数以使每对之间的绝对差异相同

给定一个由N个整数组成的数组arr[] ,任务是找到必须删除的最小数组元素数,以使每个元素对之间的绝对差相等。

例子:

方法:给定的问题可以通过计算数组元素的频率来解决,并根据以下观察结果打印结果:

  • 如果所有数组元素中的最大频率为1 ,则必须删除所有(N – 2) 个元素
  • 否则,必须删除的数组元素的最大数量为(N – 最大频率) ,使得所有数组元素都相同并且任何两对元素之间的差异相同。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
void countToMakeDiffEqual(int arr[], int n)
{
    // Stores the element having maximum
    // frequency in the array
    int ma = 0;
 
    unordered_map m;
 
    for (int i = 0; i < n; i++) {
         
        m[arr[i]]++;
 
        // Find the most occurring element
        ma = max(ma, m[arr[i]]);
    }
 
    // If only one pair exists then the
    // absolute difference between them
    // will be same
    if (n <= 2)
        cout << 0 << endl;
 
    else if (ma == 1) {
        cout << n - 2 << endl;
    }
 
    // Elements to remove is equal to the
    // total frequency minus frequency
    // of most frequent element
    else
        cout << n - ma << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 5, 1, 2, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    countToMakeDiffEqual(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.HashMap;
 
class GFG {
 
    public static void countToMakeDiffEqual(int arr[], int n)
    {
       
        // Stores the element having maximum
        // frequency in the array
        int ma = 0;
 
        HashMap m = new HashMap();
 
        for (int i = 0; i < n; i++) {
 
            // m[arr[i]]++;
            if (m.containsKey(arr[i])) {
                m.put(arr[i], m.get(arr[i]) + 1);
            } else {
                m.put(arr[i], 1);
            }
 
            // Find the most occurring element
            ma = Math.max(ma, m.get(arr[i]));
        }
 
        // If only one pair exists then the
        // absolute difference between them
        // will be same
        if (n <= 2)
            System.out.println(0);
 
        else if (ma == 1) {
            System.out.println(n - 2);
        }
 
        // Elements to remove is equal to the
        // total frequency minus frequency
        // of most frequent element
        else
            System.out.println(n - ma);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int arr[] = { 2, 5, 1, 2, 2 };
        int N = arr.length;
 
        countToMakeDiffEqual(arr, N);
    }
}
 
// This code is contributed by gfgking.


Python3
# Python 3 program for the above approach
from collections import defaultdict
 
def countToMakeDiffEqual(arr, n):
 
    # Stores the element having maximum
    # frequency in the array
    ma = 0
 
    m = defaultdict(int)
 
    for i in range(n):
 
        m[arr[i]] += 1
 
        # Find the most occurring element
        ma = max(ma, m[arr[i]])
 
    # If only one pair exists then the
    # absolute difference between them
    # will be same
    if (n <= 2):
        print(0)
 
    elif (ma == 1):
        print(n - 2)
 
    # Elements to remove is equal to the
    # total frequency minus frequency
    # of most frequent element
    else:
        print(n - ma)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [2, 5, 1, 2, 2]
    N = len(arr)
 
    countToMakeDiffEqual(arr, N)
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static void countToMakeDiffEqual(int []arr, int n)
{
    // Stores the element having maximum
    // frequency in the array
    int ma = 0;
 
    Dictionary m = new Dictionary();
 
    for (int i = 0; i < n; i++) {
        if(m.ContainsKey(arr[i]))
          m[arr[i]]++;
        else
         m.Add(arr[i],1);
 
        // Find the most occurring element
        ma = Math.Max(ma, m[arr[i]]);
    }
 
    // If only one pair exists then the
    // absolute difference between them
    // will be same
    if (n <= 2)
        Console.WriteLine(0);
 
    else if (ma == 1) {
        Console.WriteLine(n - 2);
    }
 
    // Elements to remove is equal to the
    // total frequency minus frequency
    // of most frequent element
    else
        Console.WriteLine(n - ma);
}
 
// Driver Code
public static void Main()
{
    int []arr = { 2, 5, 1, 2, 2 };
    int N = arr.Length;
 
    countToMakeDiffEqual(arr, N);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)