Sum of minimum absolute differences in an array
Given an array of n distinct integers. The task is to find the sum of minimum absolute difference of each array element. For an element arr[i] present at index i in the array, its minimum absolute difference is calculated as:
- Min absolute difference (arr[i]) = min(abs(arr[i] - arr[j])), where 0 <= j < n and j != i and abs is the absolute value.
Examples:
Input : arr = [4, 1, 5]
Output : 5
Explanation: Sum of minimum absolute differences is |4-5| + |1-4| + |5-4| = 1 + 3 + 1 = 5
Input : arr = [5, 10, 1, 4, 8, 7]
Output : 9
Explanation: Sum of minimum absolute differences is
|5-4| + |10-8| + |1-4| + |4-5| + |8-7| + |7-8| = 1 + 2 + 3 + 1 + 1 + 1 = 9
Input : arr = [12, 10, 15, 22, 21, 20, 1, 8, 9]
Output : 18
[Naive Approach] Using 2 Nested Loops - O(n^2) time and O(1) space
The idea is to iterate through each element in the array and find the minimum absolute difference between that element and any other element in the array. For each element, we check all other elements, find the smallest absolute difference, and add it to our sum.
// C++ program to find Sum of minimum absolute
// difference of each array element
#include <bits/stdc++.h>
using namespace std;
int minSumDiff(vector<int> &arr) {
int n = arr.size();
int sum = 0;
for (int i = 0; i < n; i++) {
int minDiff = INT_MAX;
for (int j = 0; j < n; j++) {
if (i != j) {
minDiff = min(minDiff, abs(arr[i] - arr[j]));
}
}
// Add minimum difference to sum
sum += minDiff;
}
return sum;
}
int main() {
vector<int> arr = {4, 1, 5};
cout << minSumDiff(arr);
return 0;
}
// Java program to find Sum of minimum absolute
// difference of each array element
class GfG {
static int minSumDiff(int[] arr) {
int n = arr.length;
int sum = 0;
for (int i = 0; i < n; i++) {
int minDiff = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (i != j) {
minDiff = Math.min(minDiff, Math.abs(arr[i] - arr[j]));
}
}
// Add minimum difference to sum
sum += minDiff;
}
return sum;
}
public static void main(String[] args) {
int[] arr = {4, 1, 5};
System.out.println(minSumDiff(arr));
}
}
# Python program to find Sum of minimum absolute
# difference of each array element
def minSumDiff(arr):
n = len(arr)
sum = 0
for i in range(n):
minDiff = float('inf')
for j in range(n):
if i != j:
minDiff = min(minDiff, abs(arr[i] - arr[j]))
# Add minimum difference to sum
sum += minDiff
return sum
if __name__ == "__main__":
arr = [4, 1, 5]
print(minSumDiff(arr))
// C# program to find Sum of minimum absolute
// difference of each array element
using System;
class GfG {
static int minSumDiff(int[] arr) {
int n = arr.Length;
int sum = 0;
for (int i = 0; i < n; i++) {
int minDiff = int.MaxValue;
for (int j = 0; j < n; j++) {
if (i != j) {
minDiff = Math.Min(minDiff, Math.Abs(arr[i] - arr[j]));
}
}
// Add minimum difference to sum
sum += minDiff;
}
return sum;
}
static void Main() {
int[] arr = {4, 1, 5};
Console.WriteLine(minSumDiff(arr));
}
}
// JavaScript program to find Sum of minimum absolute
// difference of each array element
function minSumDiff(arr) {
let n = arr.length;
let sum = 0;
for (let i = 0; i < n; i++) {
let minDiff = Number.MAX_VALUE;
for (let j = 0; j < n; j++) {
if (i !== j) {
minDiff = Math.min(minDiff, Math.abs(arr[i] - arr[j]));
}
}
// Add minimum difference to sum
sum += minDiff;
}
return sum;
}
let arr = [4, 1, 5];
console.log(minSumDiff(arr));
Output
5
[Expected Approach] Using Sorting - O(n Log n) time and O(1) space
The idea is to sort the array first so that elements with similar values are placed adjacently. After sorting, for any element, its minimum absolute difference will be with either its left neighbor or its right neighbor.
Step by step approach:
- Sort the input array to place similar elements next to each other.
- Calculate minimum difference for first element (only has right neighbor) and minimum difference for last element (only has left neighbor).
- For each middle element, find minimum of differences with left and right neighbors.
- Sum all these minimum differences to get the final result.
// C++ program to find Sum of minimum absolute
// difference of each array element
#include <bits/stdc++.h>
using namespace std;
int minSumDiff(vector<int> &arr) {
int n = arr.size();
// Sort the array
sort(arr.begin(), arr.end());
int sum = 0;
// For first element, the closest
// is the second element
sum += arr[1] - arr[0];
// For last element, the closest
// is the second-last element
sum += arr[n-1] - arr[n-2];
// For middle elements, check both
// left and right neighbors
for (int i = 1; i < n-1; i++) {
int leftDiff = arr[i] - arr[i-1];
int rightDiff = arr[i+1] - arr[i];
// Add the minimum of left
// and right differences
sum += min(leftDiff, rightDiff);
}
return sum;
}
int main() {
vector<int> arr = {4, 1, 5};
cout << minSumDiff(arr);
return 0;
}
// Java program to find Sum of minimum absolute
// difference of each array element
import java.util.Arrays;
class GfG {
static int minSumDiff(int[] arr) {
int n = arr.length;
// Sort the array
Arrays.sort(arr);
int sum = 0;
// For first element, the closest
// is the second element
sum += arr[1] - arr[0];
// For last element, the closest
// is the second-last element
sum += arr[n-1] - arr[n-2];
// For middle elements, check both
// left and right neighbors
for (int i = 1; i < n-1; i++) {
int leftDiff = arr[i] - arr[i-1];
int rightDiff = arr[i+1] - arr[i];
// Add the minimum of left
// and right differences
sum += Math.min(leftDiff, rightDiff);
}
return sum;
}
public static void main(String[] args) {
int[] arr = {4, 1, 5};
System.out.println(minSumDiff(arr));
}
}
# Python program to find Sum of minimum absolute
# difference of each array element
def minSumDiff(arr):
n = len(arr)
# Sort the array
arr.sort()
sum = 0
# For first element, the closest
# is the second element
sum += arr[1] - arr[0]
# For last element, the closest
# is the second-last element
sum += arr[n-1] - arr[n-2]
# For middle elements, check both
# left and right neighbors
for i in range(1, n-1):
leftDiff = arr[i] - arr[i-1]
rightDiff = arr[i+1] - arr[i]
# Add the minimum of left
# and right differences
sum += min(leftDiff, rightDiff)
return sum
if __name__ == "__main__":
arr = [4, 1, 5]
print(minSumDiff(arr))
// C# program to find Sum of minimum absolute
// difference of each array element
using System;
class GfG {
static int minSumDiff(int[] arr) {
int n = arr.Length;
// Sort the array
Array.Sort(arr);
int sum = 0;
// For first element, the closest
// is the second element
sum += arr[1] - arr[0];
// For last element, the closest
// is the second-last element
sum += arr[n-1] - arr[n-2];
// For middle elements, check both
// left and right neighbors
for (int i = 1; i < n-1; i++) {
int leftDiff = arr[i] - arr[i-1];
int rightDiff = arr[i+1] - arr[i];
// Add the minimum of left
// and right differences
sum += Math.Min(leftDiff, rightDiff);
}
return sum;
}
static void Main() {
int[] arr = {4, 1, 5};
Console.WriteLine(minSumDiff(arr));
}
}
// JavaScript program to find Sum of minimum absolute
// difference of each array element
function minSumDiff(arr) {
let n = arr.length;
// Sort the array
arr.sort((a, b) => a - b);
let sum = 0;
// For first element, the closest
// is the second element
sum += arr[1] - arr[0];
// For last element, the closest
// is the second-last element
sum += arr[n-1] - arr[n-2];
// For middle elements, check both
// left and right neighbors
for (let i = 1; i < n-1; i++) {
let leftDiff = arr[i] - arr[i-1];
let rightDiff = arr[i+1] - arr[i];
// Add the minimum of left
// and right differences
sum += Math.min(leftDiff, rightDiff);
}
return sum;
}
let arr = [4, 1, 5];
console.log(minSumDiff(arr));
Output
5