- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

This program is to find minimum absolute difference of the array given we have an array which have distinct element.To learn this concept better let’s rebrush the things that required,

**Array** is a container of elements of same data type. The length of an array needs to be predefined.

**absolute difference** is the absolute value of the difference between two numbers i.e. the difference will always be positive, negative values will be converted to positive.

The sum of minimum absolute difference of each element has to be found the minimum absolute solute difference formula is

**Minimum Absolute Difference (a) = min(abs(a – arr[j])) ;**

where 1 <= j <= n and j != i, **abs** is the absolute value.

Input: arr = {1, 3, 9, 3, 6} Output: 8

The optimal solution is to choose x = 3, which produces the sum

|1 – 3| + |3 – 3| + |9 – 3| + |3 – 3| = 2 + 0 + 6 + 0 = 8

The given input array is sorted.

minimum absolute difference for the

**first**element of the array, is calculated using the second array element.minimum absolute difference for the

**last**array element, is also calculated using the second last array element., minimum absolute difference for the other array elements which hare present at index i are calculated by :

**minAbsDiff**= min( abs(arr[i] – arr[i-1]), abs(ar[i] – arr[i+1]) ).

#include<iostream> #include <algorithm> using namespace std; int abs_sum(int a[], int len); int main() { int a[]={1, 3, 9, 3, 6}; int n, i; n=5; sort(a, a+n); int sum = 0; sum += abs(a[0]- a[1]); sum += abs(a[n-1]-a[n-2]); for (int i = 1; i < n-1; i++) { sum += min(abs(a[i]-a[i-1]), abs(a[i]-a[i+1])); } cout<<"The element with minimum sum of absolute differences is : "<<sum; return 0; }

The element with minimum sum of absolute differences is : 8

- Related Questions & Answers
- Array element with minimum sum of absolute differences?
- Program to find sum of absolute differences in a sorted array in Python
- Absolute sum of array elements - JavaScript
- Program to find minimum absolute sum difference in Python
- Taking the absolute sum of Array of Numbers in JavaScript
- Sum of subset differences in C++
- Maximum sum of absolute difference of any permutation in C++
- Minimum Sum Path In 3-D Array in C++
- Minimum removals to make array sum even in C++
- Minimum removals to make array sum odd in C++
- Minimum sum of two numbers formed from digits of an array in C++
- Minimum sum submatrix in a given 2D array in C++
- Minimum Path Sum in C++
- Find an element in array such that sum of left array is equal to sum of right array using c++
- Find a number which give minimum sum when XOR with every number of array of integer in C++

Advertisements