Skip to content
Related Articles
Find the array element having minimum sum of absolute differences with all other array elements
• Difficulty Level : Easy
• Last Updated : 18 Jun, 2021

Given an array arr[] of size N, the task is to find the minimum sum of absolute differences of an array element with all elements of another array.

Input: arr[ ] = {1, 2, 3, 4, 5}, N = 5
Output: 3
Explanation:
For arr[0](= 1): Sum = abs(2 – 1) + abs(3 – 1) + abs(4 – 1) + abs(5 – 1) = 10.
For arr[1](= 2): Sum = abs(2 – 1) + abs(3 – 2) + abs(4 – 2) + abs(5 – 2) = 7.
For arr[2](= 3): Sum = abs(3 – 1) + abs(3 – 2) + abs(4 – 3) + abs(5 – 3) = 6 (Minimum).
For arr[3](= 4): Sum = abs(4 – 1) + abs(4 – 2) + abs(4 – 3) + abs(5 – 4) = 7.
For arr[0](= 1): Sum = 10.

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

Approach: The problem can be solved based on the observation that the sum of absolute differences of all array elements is minimum for the median of the array. Follow the steps below to solve the problem:

• Sort the array arr[].
• Print the middle element of the sorted array as the required answer.

Below is the implementation of the above approach:

C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to return the array element``// having minimum sum of absolute``// differences with other array elements``void` `minAbsDiff(``int` `arr[], ``int` `n)``{` `    ``// Sort the array``    ``sort(arr, arr + n);` `    ``// Print the middle element``    ``cout << arr[n / 2] << endl;``}` `// Driver Code``int` `main()``{` `    ``int` `n = 5;``    ``int` `arr[] = { 1, 2, 3, 4, 5 };` `    ``minAbsDiff(arr, n);` `    ``return` `0;``}`

Java

 `// Java program for the above approach``import` `java.util.Arrays;``public` `class` `GFG``{``  ` `  ``// Function to return the array element``  ``// having minimum sum of absolute``  ``// differences with other array elements``  ``static` `void` `minAbsDiff(``int` `arr[], ``int` `n)``  ``{` `    ``// Sort the array``    ``Arrays.sort(arr);` `    ``// Print the middle element``    ``System.out.println(arr[n / ``2``]);``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int` `n = ``5``;``    ``int` `arr[] = { ``1``, ``2``, ``3``, ``4``, ``5` `};``    ``minAbsDiff(arr, n);``  ``}``}` `// This code is contributed by abhinavjain194`

Python3

 `# Python3 program for the above approach` `# Function to return the array element``# having minimum sum of absolute``# differences with other array elements``def` `minAbsDiff(arr, n):` `    ``# Sort the array``    ``arr.sort()` `    ``# Print the middle element``    ``print``(arr[n ``/``/` `2``])` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `5``    ``arr ``=` `[ ``1``, ``2``, ``3``, ``4``, ``5` `]``    ` `    ``minAbsDiff(arr, n)` `# This code is contributed by SURENDRA_GANGWAR`

C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{``  ` `// Function to return the array element``// having minimum sum of absolute``// differences with other array elements``static` `void` `minAbsDiff(``int` `[]arr, ``int` `n)``{``    ` `    ``// Sort the array``    ``Array.Sort(arr);``    ` `    ``// Print the middle element``    ``Console.WriteLine(arr[n / 2]);``}` `// Driver code``public` `static` `void` `Main(``string``[] args)``{``    ``int` `n = 5;``    ``int` `[]arr = { 1, 2, 3, 4, 5 };``    ` `    ``minAbsDiff(arr, n);``}``}` `// This code is contributed by ankThon`

Javascript

 ``
Output:
`3`

Time Complexity: O(NlogN)
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up