Minimum sum of differences with an element in an array | Java 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
import java . lang . * ; import java . util . * ; public class GFG { static int absSumDidd ( int a [ ] ) { Arrays . sort ( a ) ; int midValue = a [ a . length / 2 ] ; int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { sum = sum + Math . abs ( a [ i ] - midValue ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 11 , 14 , 10 , 17 , 15 } ; System . out . print ( absSumDidd ( arr ) ) ; }