Find the point on X-axis from given N points having least Sum of Distances from all other points

• Last Updated : 23 Apr, 2021

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 all other points.

Example:

Input: arr[] = {4, 1, 5, 10, 2}
Output: (4, 0)
Explanation:
Distance of 4 from rest of the elements = |4 – 1| + |4 – 5| + |4 – 10| + |4 – 2| = 12
Distance of 1 from rest of the elements = |1 – 4| + |1 – 5| + |1 – 10| + |1 – 2| = 17
Distance of 5 from rest of the elements = |5 – 1| + |5 – 4| + |5 – 2| + |5 – 10| = 13
Distance of 10 from rest of the elements = |10 – 1| + |10 – 2| + |10 – 5| + |10 – 4| = 28
Distance of 2 from rest of the elements = |2 – 1| + |2 – 4| + |2 – 5| + |2 – 10| = 14
Input: arr[] = {3, 5, 7, 10}
Output:

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(N2
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 an 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)th element.
• Otherwise, return the (N / 2)th element.

Below is the implementation of the above approach:

C++

 // C++ Program to implement// the above approach#include using namespace std; // Function to find median of the arrayint 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 Codeint main(){     int A[] = { 4, 1, 5, 10, 2 };    int N = sizeof(A) / sizeof(A);    cout << "(" << findLeastDist(A, N)         << ", " << 0 << ")";     return 0;}

Java

 // Java program to implement// the above approachimport java.util.*; class GFG{ // Function to find median of the arraystatic 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 Codepublic 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

Python3

 # Python3 program to implement# the above approach # Function to find median of the arraydef 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 CodeA = [4, 1, 5, 10, 2];N = len(A); print("(" , findLeastDist(A, N),      ", " , 0 , ")"); # This code is contributed by PrinciRaj1992

C#

 // C# program to implement// the above approachusing System; class GFG{ // Function to find median of the arraystatic 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 Codepublic 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

Javascript


Output:
(4, 0)

Time Complexity: O(Nlog(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up