Given an array of n distinct integers. The problem is to find the sum of minimum absolute difference of each array element. For an element x present at index i in the array its minimum absolute difference is calculated as:
Min absolute difference (x) = min(abs(x – arr[j])), where 1 <= j <= n and j != i and abs is the absolute value.
Input Constraint: 2 <= n
Examples:
Input : arr = {4, 1, 5}
Output : 5
Sum of absolute differences is |4-5| + |1-4| + |5-4|
Input : arr = {5, 10, 1, 4, 8, 7}
Output : 9
Input : {12, 10, 15, 22, 21, 20, 1, 8, 9}
Output : 18
Naive approach: Using two loops. Pick an element of the array using outer loop and calculate its absolute difference with rest of the array elements using inner loop. Find the minimum absolute value and add it to the sum. Time Complexity O(n2).
Efficient Approach: The following steps are:
- Sort the array of size n.
- For the 1st element of array its min absolute difference is calculated using the 2nd array element.
- For the last array element its min absolute difference is calculated using the 2nd last array element.
- For the rest of the array elements, 1 <= i <= n-2, minimum absolute difference for an element at index i is calculated as: minAbsDiff = min( abs(arr[i] – arr[i-1]), abs(ar[i] – arr[i+1]) ).
C++
// C++ implementation to find the sum of minimum // absolute difference of each array element #include <bits/stdc++.h> using namespace std; // function to find the sum of // minimum absolute difference int sumOfMinAbsDifferences(int arr[], int n) { // sort the given array sort(arr, arr+n); // initialize sum int sum = 0; // min absolute difference for // the 1st array element sum += abs(arr[0] - arr[1]); // min absolute difference for // the last array element sum += abs(arr[n-1] - arr[n-2]); // find min absolute difference for rest of the // array elements and add them to sum for (int i=1; i<n-1; i++) sum += min(abs(arr[i] - arr[i-1]), abs(arr[i] - arr[i+1])); // required sum return sum; } // Driver program to test above int main() { int arr[] = {5, 10, 1, 4, 8, 7}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Sum = " << sumOfMinAbsDifferences(arr, n); } |
Java
// java implementation to find the sum // of minimum absolute difference of // each array element import java.*; import java.util.Arrays; public class GFG { // function to find the sum of // minimum absolute difference static int sumOfMinAbsDifferences( int arr[] ,int n) { // sort the given array Arrays.sort(arr); // initialize sum int sum = 0; // min absolute difference for // the 1st array element sum += Math.abs(arr[0] - arr[1]); // min absolute difference for // the last array element sum += Math.abs(arr[n-1] - arr[n-2]); // find min absolute difference for // rest of the array elements and // add them to sum for (int i = 1; i < n - 1; i++) sum += Math.min(Math.abs(arr[i] - arr[i-1]), Math.abs(arr[i] - arr[i+1])); // required sum return sum; } // Driver code public static void main(String args[]) { int arr[] = {5, 10, 1, 4, 8, 7}; int n = arr.length; System.out.println( "Sum = " + sumOfMinAbsDifferences(arr, n)); } } // This code is contributed by Sam007. |
Python3
# Python implementation to find the # sum of minimum absolute difference # of each array element # function to find the sum of # minimum absolute difference def sumOfMinAbsDifferences(arr,n): # sort the given array arr.sort() # initialize sum sum = 0 # min absolute difference for # the 1st array element sum += abs(arr[0] - arr[1]); # min absolute difference for # the last array element sum += abs(arr[n - 1] - arr[n - 2]); # find min absolute difference for # rest of the array elements and # add them to sum for i in range(1, n - 1): sum += min(abs(arr[i] - arr[i - 1]), abs(arr[i] - arr[i + 1])) # required sum return sum; # Driver code arr = [5, 10, 1, 4, 8, 7] n = len(arr) print( "Sum = ", sumOfMinAbsDifferences(arr, n)) #This code is contributed by Sam007 |
C#
// C# implementation to find the sum // of minimum absolute difference of // each array element using System; public class GFG { // function to find the sum of // minimum absolute difference static int sumOfMinAbsDifferences( int []arr ,int n) { // sort the given array Array.Sort(arr); // initialize sum int sum = 0; // min absolute difference for // the 1st array element sum += Math.Abs(arr[0] - arr[1]); // min absolute difference for // the last array element sum += Math.Abs(arr[n-1] - arr[n-2]); // find min absolute difference for // rest of the array elements and // add them to sum for (int i = 1; i < n - 1; i++) sum += Math.Min(Math.Abs(arr[i] - arr[i-1]), Math.Abs(arr[i] - arr[i+1])); // required sum return sum; } // Driver code public static void Main () { int []arr = {5, 10, 1, 4, 8, 7}; int n = arr.Length; Console.Write( "Sum = " + sumOfMinAbsDifferences(arr, n)); } } // This code is contributed by Sam007. |
PHP
<?php // PHP implementation to find // the sum of minimum absolute // difference of each array element // function to find the sum of // minimum absolute difference function sumOfMinAbsDifferences($arr, $n) { // sort the given array sort($arr); sort( $arr,$n); // initialize sum $sum = 0; // min absolute difference for // the 1st array element $sum += abs($arr[0] - $arr[1]); // min absolute difference for // the last array element $sum += abs($arr[$n - 1] - $arr[$n - 2]); // find min absolute difference // for rest of the array elements // and add them to sum for ($i = 1; $i < $n - 1; $i++) $sum += min(abs($arr[$i] - $arr[$i - 1]), abs($arr[$i] - $arr[$i + 1])); // required sum return $sum; } // Driver Code $arr = array(5, 10, 1, 4, 8, 7); $n = sizeof($arr); echo "Sum = ", sumOfMinAbsDifferences($arr, $n); // This code is contributed by nitin mittal. ?> |
Output:
Sum = 9
Time Complexity: O(nlogn)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
Recommended Posts:
- Form an array of distinct elements with each element as sum of an element from each array
- Sum of product of each element with each element after it
- Array element with minimum sum of absolute differences
- Array formed from difference of each element from the largest element in the given array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Replace each element by the difference of the total size of the array and frequency of that element
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Minimum sum of absolute difference of pairs of two arrays
- Minimum pair sum operations to make array each element divisible by 4
- Find just strictly greater element from first array for each element in second array
- Array formed using sum of absolute differences of that element with all other elements
- Minimum absolute difference of adjacent elements in a circular array
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Count of all pairs in an Array with minimum absolute difference
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Generate Array whose difference of each element with its left yields the given Array
- Check if each element of an Array is the Sum of any two elements of another Array

