Minimum sum of absolute difference of pairs of two arrays | C # program to find minimum sum of absolute differences of two arrays . ; Returns minimum possible pairwise absolute difference of two arrays . ; Sort both arrays ; Find sum of absolute differences ; Driver code ; Both a [ ] and b [ ] must be of same size .
using System ; class MinSum { static long findMinSum ( long [ ] a , long [ ] b , long n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + Math . Abs ( a [ i ] - b [ i ] ) ; return sum ; } public static void Main ( String [ ] args ) { long [ ] a = { 4 , 1 , 8 , 7 } ; long [ ] b = { 2 , 3 , 6 , 5 } ; int n = a . Length ; Console . Write ( findMinSum ( a , b , n ) ) ; } }