Related Articles

Count of array elements to be removed to make absolute difference between each pair same

• Last Updated : 17 Sep, 2021

Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements that must be removed such that the absolute difference between each element pair is equal.

Examples:

Input: arr[] = {1, 2}
Output: 0
Explanation: There is only one pair of integers with absolute difference | arr[1] − arr[2] | = | 1- 2 | = 1. So there is no need to delete any integer from the given array.

Input: arr[] = {2, 5, 1, 2, 2}
Output: 2
Explanation: After deleting 1 and 5, the array A becomes [2, 2, 2] and the absolute difference between each pair of integers is 0.

Approach: The given problem can be solved by counting the frequencies of array elements and print the result based on the following observations:

• If the maximum frequency among all the array elements is 1, then all (N – 2) elements must be removed.
• Otherwise, the maximum number of array elements that must be removed is (N – maximum frequency) such that all array elements are the same and the difference between any two pairs of elements is the same.

Below is the implementation of the above approach:

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<``int``, ``int``> m;` `    ``for` `(``int` `i = 0; i < n; i++) {``        ` `        ``m[arr[i]]++;` `        ``// Find the most occuring 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;``}`

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 occuring 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.`

Javascript

 ``
Output:
`2`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up