Given an array **arr[] **consisting of **N **integers, denoting **N** points lying on the **X-axis**, the task is to find the point which has the **least sum of distances** from the all other points.

**Example:**

Input:arr[] = {4, 1, 5, 10, 2}Output:(4, 0)Explanation:

Distance of4from rest of the elements = |4 – 1| + |4 – 5| + |4 – 10| + |4 – 2| = 12

Distance of1from rest of the elements = |1 – 4| + |1 – 5| + |1 – 10| + |1 – 2| = 17

Distance of5from rest of the elements = |5 – 1| + |5 – 4| + |5 – 2| + |5 – 10| = 13

Distance of10from rest of the elements = |10 – 1| + |10 – 2| + |10 – 5| + |10 – 4| = 28

Distance of2from rest of the elements = |2 – 1| + |2 – 4| + |2 – 5| + |2 – 10| = 14Input:arr[] = {3, 5, 7, 10}Output:5

**Naive Approach:**

The task is to iterate over the array, and for each array element, calculate the sum of its absolute difference with all other array elements. Finally, print the array element with the maximum sum of differences. **Time Complexity:** O(N^{2}) **Auxiliary Space:** O(1)

**Efficient Approach: **To optimize the above approach, the idea is to find the median of the array. The median of the array will have the least possible total distance from other elements in the array. For an array with even number of elements, there are two possible medians and both will have the same total distance, return the one with the lower index since it is closer to origin.

Follow the below steps to solve the problem:

- Sort the given array.
- If
**N**is**odd**, return the**(N + 1 / 2)**element.^{th } - Otherwise, return the
**(N / 2)**element.^{th }

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find median of the array ` `int` `findLeastDist(` `int` `A[], ` `int` `N) ` `{ ` ` ` `// Sort the given array ` ` ` `sort(A, A + N); ` ` ` ` ` `// If number of elements are even ` ` ` `if` `(N % 2 == 0) { ` ` ` ` ` `// Return the first median ` ` ` `return` `A[(N - 1) / 2]; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `else` `{ ` ` ` `return` `A[N / 2]; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `A[] = { 4, 1, 5, 10, 2 }; ` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `cout << ` `"("` `<< findLeastDist(A, N) ` ` ` `<< ` `", "` `<< 0 << ` `")"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find median of the array ` `static` `int` `findLeastDist(` `int` `A[], ` `int` `N) ` `{ ` ` ` ` ` `// Sort the given array ` ` ` `Arrays.sort(A); ` ` ` ` ` `// If number of elements are even ` ` ` `if` `(N % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` ` ` `// Return the first median ` ` ` `return` `A[(N - ` `1` `) / ` `2` `]; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `{ ` ` ` `return` `A[N / ` `2` `]; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A[] = { ` `4` `, ` `1` `, ` `5` `, ` `10` `, ` `2` `}; ` ` ` `int` `N = A.length; ` ` ` ` ` `System.out.print(` `"("` `+ findLeastDist(A, N) + ` ` ` `", "` `+ ` `0` `+ ` `")"` `); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to find median of the array ` `def` `findLeastDist(A, N): ` ` ` ` ` `# Sort the given array ` ` ` `A.sort(); ` ` ` ` ` `# If number of elements are even ` ` ` `if` `(N ` `%` `2` `=` `=` `0` `): ` ` ` ` ` `# Return the first median ` ` ` `return` `A[(N ` `-` `1` `) ` `/` `/` `2` `]; ` ` ` ` ` `# Otherwise ` ` ` `else` `: ` ` ` `return` `A[N ` `/` `/` `2` `]; ` ` ` `# Driver Code ` `A ` `=` `[` `4` `, ` `1` `, ` `5` `, ` `10` `, ` `2` `]; ` `N ` `=` `len` `(A); ` ` ` `print` `(` `"("` `, findLeastDist(A, N), ` ` ` `", "` `, ` `0` `, ` `")"` `); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find median of the array ` `static` `int` `findLeastDist(` `int` `[]A, ` `int` `N) ` `{ ` ` ` ` ` `// Sort the given array ` ` ` `Array.Sort(A); ` ` ` ` ` `// If number of elements are even ` ` ` `if` `(N % 2 == 0) ` ` ` `{ ` ` ` ` ` `// Return the first median ` ` ` `return` `A[(N - 1) / 2]; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `{ ` ` ` `return` `A[N / 2]; ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `[]A = { 4, 1, 5, 10, 2 }; ` ` ` `int` `N = A.Length; ` ` ` ` ` `Console.Write(` `"("` `+ findLeastDist(A, N) + ` ` ` `", "` `+ 0 + ` `")"` `); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

(4, 0)

**Time Complexity:** O(Nlog(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.

## Recommended Posts:

- Minimum Sum of Euclidean Distances to all given Points
- Sum of the distances from every node to all other nodes is maximum
- Find a point such that sum of the Manhattan distances is minimized
- Sum of Manhattan distances between all pairs of points
- Smallest character in a string having minimum sum of distances between consecutive repetitions
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Sum of all distances between occurrences of same characters in a given string
- Find the original coordinates whose Manhattan distances are given
- Find the other end point of a line with given one end and mid
- Find the distance covered to collect items at equal distances
- C/C++ program to add N distances given in inch-feet system using Structures
- Count all disjoint pairs having absolute difference at least K from a given array
- First subarray having sum at least half the maximum sum of any subarray of size K
- Check if the given point lies inside given N points of a Convex Polygon
- Pair having all other given pairs lying between its minimum and maximum
- Find elements in a given range having at least one odd divisor
- Maximum integral co-ordinates with non-integer distances
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Find the number of points that have atleast 1 point above, below, left or right of it
- Find if a point lies inside, outside or on the circumcircle of three points A, B, C

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.