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

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



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

edit
close

play_arrow

link
brightness_4
code

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

edit
close

play_arrow

link
brightness_4
code

# 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

edit
close

play_arrow

link
brightness_4
code

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

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.





Check out this Author's contributed articles.

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.



Improved By : princiraj1992, rutvik_56

Article Tags :