Array element with minimum sum of absolute differences | Set 2
Last Updated :
27 Apr, 2021
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: 3
Explanation:
- For element arr[0](= 1): |(1 – 1)| + |(2 – 1)| + |(3 – 1)| + |(4 – 1)| + |(5 – 1)| = 0 + 1 + 2 + 3 + 4 = 10.
- For element arr[1](= 2): |(1 – 2)| + |(2 – 2)| + |(3 – 2)| + |(4 – 2)| + |(5 – 2)| = 1 + 0 + 1 + 2 + 3 = 7.
- For element arr[2](= 3): |(1 – 3)| + |(2 – 3)| + |(3 – 3)| + |(4 – 3)| + |(5 – 3)| = 2 + 1 + 0 + 1 + 2 = 6.
- For element arr[3](= 4): |(1 – 4)| + |(2 – 4)| + |(3 – 4)| + |(4 – 4)| + |(5 – 4)| = 3 + 2 + 1 + 0 + 1 = 7.
- For element arr[4](= 5): |(1 – 5)| + |(2 – 5)| + |(3 – 5)| + |(4 – 5)| + |(5 – 5)| = 4 + 3 + 2 + 1 + 0 = 10.
Therefore, the element having minimum sum of absolute differences with all array elements is 3.
Input: arr[] = {15, 12, 13, 10}
Output: 12
Naive Approach: The simplest approach to solve the given problem is to find the sum of absolute differences of array elements with every element of the array one by one, and print that element that has the smaller sum of differences.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Median-based Approach: Refer to the previous post of this article to solve this problem using median-finding technique.
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by minimizing the value of (sum of all the array elements – X*N) for each array element X and find the resultant element X. Follow the steps below to solve the problem:
- Initialize a variable, say res as arr[0] that stores the resultant array element whose sum of absolute differences of array elements with res is minimum.
- Find the sum of the array elements and store it in the variable, say sum.
- Initialize a variable, say minDiff as the value of the sum.
- Traverse the given array arr[] and if the absolute value of (sum – (arr[i] * N)) is less than minDiff then update minDiff to this value and res as the current array element i.e., arr[i].
- After completing the above steps, print the value of res as the resultant array element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumDiff( int arr[], int N)
{
int res = arr[0], sum = 0;
for ( int i = 0; i < N; i++)
sum += arr[i];
int min_diff = sum;
for ( int i = 0; i < N; i++) {
if ( abs (sum - (arr[i] * N))
< min_diff) {
min_diff = abs (sum - (arr[i] * N));
res = arr[i];
}
}
cout << res;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumDiff(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void minimumDiff( int [] arr, int N)
{
int res = arr[ 0 ], sum = 0 ;
for ( int i = 0 ; i < N; i++)
sum += arr[i];
int min_diff = sum;
for ( int i = 0 ; i < N; i++)
{
if (Math.abs(sum - (arr[i] * N)) < min_diff)
{
min_diff = Math.abs(sum - (arr[i] * N));
res = arr[i];
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
minimumDiff(arr, N);
}
}
|
C#
using System;
class GFG{
static void minimumDiff( int [] arr, int N)
{
int res = arr[0], sum = 0;
for ( int i = 0; i < N; i++)
sum += arr[i];
int min_diff = sum;
for ( int i = 0; i < N; i++)
{
if (Math.Abs(sum - (arr[i] * N)) < min_diff)
{
min_diff = Math.Abs(sum - (arr[i] * N));
res = arr[i];
}
}
Console.Write(res);
}
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
minimumDiff(arr, N);
}
}
|
Python3
def minimumDiff(arr, N):
res = arr[ 0 ]
sum1 = 0
for i in range (N):
sum1 + = arr[i]
min_diff = sum1
for i in range (N):
if ( abs (sum1 - (arr[i] * N)) < min_diff):
min_diff = abs (sum1 - (arr[i] * N))
res = arr[i]
print (res)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
minimumDiff(arr, N)
|
Javascript
<script>
function minimumDiff(arr, N)
{
let res = arr[0], sum = 0;
for (let i = 0; i < N; i++)
sum += arr[i];
let min_diff = sum;
for (let i = 0; i < N; i++) {
if (Math.abs(sum - (arr[i] * N))
< min_diff) {
min_diff = Math.abs(sum - (arr[i] * N));
res = arr[i];
}
}
document.write(res);
}
let arr = [ 1, 2, 3, 4, 5 ];
let N = arr.length;
minimumDiff(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...