# Sum of minimum absolute difference of each array element

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

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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:

1. Sort the array of size n.
2. For the 1st element of array its min absolute difference is calculated using the 2nd array element.
3. For the last array element its min absolute difference is calculated using the 2nd last array element.
4. 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 ` `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 - arr); ` `     `  `    ``// 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

## 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 - arr); ` `         `  `        ``// 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

 ` `

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.

My Personal Notes arrow_drop_up

Improved By : Sam007, nitin mittal

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.