The program to find the sum of the minimum absolute difference of the array is discussed here. An array of distinct elements is given as input and the sum of the minimum absolute difference of each array element has to be found. The minimum absolute difference is calculated using the formula,
Minimum Absolute Difference (a) = min(abs(a arr[j])) ; where 1 <= j <= n and j != i, abs is the absolute value.
For example, consider the following array given as input: arr = {1, 3, 9, 3, 6}
The optimal solution is to choose x = 3, which produces the sum
|1 3| + |3 3| + |9 3| + |3 3| + |6 3| = 2 + 0 + 6 + 0 + 3 = 11
Output: 11
@@coding::1@@
Time complexity: O(n)
Recommended Programs