Find the array element having minimum sum of absolute differences with all other array elements Last Updated : 24 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 5Output: 3Explanation: 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 = 4Output: 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 <bits/stdc++.h> 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 <script> // JavaScript program for the above approach // Function to return the array element // having minimum sum of absolute // differences with other array elements function minAbsDiff(arr, n){ // Sort the array arr.sort(function(a, b){return a-b}) // Print the middle element document.write(arr[(Math.floor(n / 2))]) } // main code let n = 5 let arr = [ 1, 2, 3, 4, 5 ] minAbsDiff(arr, n) // This code is contributed by AnkThon </script> Output: 3 Time Complexity: O(NlogN) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find all pairs in an Array in sorted order with minimum absolute difference U ujjwalgoel1103 Follow Improve Article Tags : DSA median-finding Similar Reads Array formed using sum of absolute differences of that element with all other elements Given a sorted array arr[] of N distinct positive integers. The task is to generate an array such that the element at each index in the new array is the sum of absolute differences of the corresponding element with all other elements of the given array. Input: arr[] = [2, 3, 5]Output: [4, 3, 5]Expla 11 min read Array element with minimum sum of absolute differences | Set 2 Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4 7 min read Find all pairs in an Array in sorted order with minimum absolute difference Given an integer array arr[] of size N, the task is to find all distinct pairs having minimum absolute difference and print them in ascending order. Examples:Input: arr[] = {4, 2, 1, 3}Output: {1, 2}, {2, 3}, {3, 4}Explanation: The minimum absolute difference between pairs is 1.Input: arr[] = {1, 3, 9 min read Maximize the absolute difference for all elements in the array Given an array A[] of size N and B[] of size M (M >= N), the task is to construct an array C[] by choosing N integers from B[] such that for every index i, sum of absolute difference between A[i] and C[i] is maximum. Examples: Input: N = 4, M = 6, A[] = {6, 1, 2, 4}, B[] = {3, 5, 1, 7, 2, 3}Outpu 10 min read Split array into K subarrays with minimum sum of absolute difference between adjacent elements Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray. Examples: Input: arr[] = {1, 3, -2, 5, -1}, K = 2Output: 13Explanation: Split the array into following 2 subarrays: 8 min read Find maximum absolute difference with min sum of ratios in an Array Given an array of positive integers arr[], the task is to find the maximum absolute difference between the pairs from the array such that the sum of their ratios is minimum. Examples: Input: arr[] = {2, 6, 3, 4, 8, 9}Output: 4Explanation: In the above example, the ratios of every pair are calculated 7 min read Like