Minimum sum of differences with an element in an array | C ++ program to find minimum sum of absolute differences with an array element . ; function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int absSumDidd ( int a [ ] , int n ) { sort ( a , a + n ) ; int midValue = a [ ( int ) ( n / 2 ) ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + abs ( a [ i ] - midValue ) ; } return sum ; } int main ( ) { int arr [ ] = { 5 , 11 , 14 , 10 , 17 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << absSumDidd ( arr , n ) ; }