Minimum sum of absolute difference of pairs of two arrays | Returns minimum possible pairwise absolute difference of two arrays . ; Sort both arrays ; Find sum of absolute differences ; Both a [ ] and b [ ] must be of same size .
function findMinSum ( a , b , n ) { a . sort ( ) ; b . sort ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum = sum + Math . abs ( a [ i ] - b [ i ] ) ; return sum ; } let a = [ 4 , 1 , 8 , 7 ] ; let b = [ 2 , 3 , 6 , 5 ] ; let n = a . length ; document . write ( findMinSum ( a , b , n ) ) ;