Sum of minimum absolute differences in an array
Last Updated :
24 Apr, 2025
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++
// 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
// 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
# 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#
// 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
// 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));
[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++
// 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
// 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
# 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#
// 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
// 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));
Similar Reads
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
Count of all pairs in an Array with minimum absolute difference Given an integer array arr[] of size N, the task is to count the total number of distinct pairs having minimum absolute difference. Examples: Input: arr[] = {4, 2, 1, 3} Output: 3 Explanation: The minimum absolute difference between the pairs {1, 2}, {2, 3}, {3, 4} is 1.Input: arr[] = {1, 3, 8, 10,
5 min read
Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[]
11 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
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read
Maximum absolute difference of value and index sums Given an unsorted array A of N integers, A_{1}, A_{2}, ...., A_{N}. Return maximum value of f(i, j) for all 1 ? i, j ? N. f(i, j) or absolute difference of two elements of an array A is defined as |A[i] - A[j]| + |i - j|, where |A| denotes the absolute value of A. Examples: We will calculate the val
14 min read
Minimum sum of absolute difference of pairs of two arrays Given two arrays a[] and b[] of equal length n. The task is to pair each element of array a to an element in array b, such that sum S of absolute differences of all the pairs is minimum.Suppose, two elements a[i] and a[j] (i != j) of a are paired with elements b[p] and b[q] of b respectively, then p
7 min read
Minimum absolute difference of adjacent elements in a Circular Array Given a circular array arr[] of length N, the task is to find the minimum absolute difference between any adjacent pair. If there are many optimum solutions, output any of them. Examples: Input: arr[] = {10, 12, 13, 15, 10} Output: 0Explanation: |10 - 10| = 0 is the minimum possible difference. Inpu
5 min read
Minimum possible sum of absolute difference of pairs from given arrays Given two arrays a[] and b[] of size N and M respectively (N < M), the task is to find the minimum possible sum of absolute difference of pairs formed by pairing each element of array a[] with an element of array b[] Note: Each element of each array can be considered only once. Examples: Input: a
8 min read
Minimum sum of absolute differences between pairs of a triplet from an array Given an array A[] consisting of positive integers, the task is to find the minimum value of |A[x] - A[y]| + |A[y] - A[z]| of any triplet (A[x], A[y], A[z]) from an array. Examples: Input: A[] = { 1, 1, 2, 3 }Output: 1Explanation:For x = 0, y = 1, z = 2|A[x] - A[y]| + |A[y] - A[z]| = 0 + 1 = 1, whic
5 min read