# Array element with minimum sum of absolute differences

Given an array **arr[]** of **N** integers, the task is to find an element **x** from the array such that **|arr[0] – x| + |arr[1] – x| + |arr[2] – x| + … + |arr[n – 1] – x|** is minimized, then print the minimized sum.

**Examples:**

Input:arr[] = {1, 3, 9, 3, 6}

Output:11

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

Input:arr[] = {1, 2, 3, 4}

Output:4

A **simple solution** is to iterate through every element and check if it gives optimal solution or not. Time Complexity of this solution is O(n*n)

An **Efficient Approach:** is to always pick **x** as the median of the array. If **n is even** and there are **two medians** then both the medians are optimal choices. The time complexity for the approach is **O(n * log(n))** because the array will have to be sorted in order to find the median. Calculate and print the minimized sum when x is found (median of the array).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimized sum ` `int` `minSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Sort the array ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Median of the array ` ` ` `int` `x = arr[n / 2]; ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// Calculate the minimized sum ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += ` `abs` `(arr[i] - x); ` ` ` ` ` `// Return the required sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 3, 9, 3, 6 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << minSum(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimized sum ` `def` `minSum(arr, n) : ` ` ` ` ` `# Sort the array ` ` ` `arr.sort(); ` ` ` ` ` `# Median of the array ` ` ` `x ` `=` `arr[n ` `/` `/` `2` `]; ` ` ` ` ` `sum` `=` `0` `; ` ` ` ` ` `# Calculate the minimized sum ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `sum` `+` `=` `abs` `(arr[i] ` `-` `x); ` ` ` ` ` `# Return the required sum ` ` ` `return` `sum` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `1` `, ` `3` `, ` `9` `, ` `3` `, ` `6` `]; ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(minSum(arr, n)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

11

The time complexity of the above solution is O(n Log n). We can further optimize it to work in O(n) using linear time algortihm to find k-th largest element.

## Recommended Posts:

- Minimum sum of differences with an element in an array
- Minimum and Maximum sum of absolute differences of pairs
- Sum of minimum absolute difference of each array element
- Sum of absolute differences of all pairs in a given array
- Clustering/Partitioning an array such that sum of square differences is minimum
- Minimum absolute difference of adjacent elements in a circular array
- Remove exactly one element from the array such that max - min is minimum
- Minimum array element changes to make its elements 1 to N
- Sum and Product of minimum and maximum element of an Array
- Minimum and Maximum element of an array which is divisible by a given number k
- Find the minimum element in a sorted and rotated array
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Minimum element whose n-th power is greater than product of an array of size n
- Search an element in an unsorted array using minimum number of comparisons
- Minimum pair sum operations to make array each element divisible by 4

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.