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 .
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findMinSum ( long long int a [ ] , long long int b [ ] , int n ) { sort ( a , a + n ) ; sort ( b , b + n ) ; long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + abs ( a [ i ] - b [ i ] ) ; return sum ; } int main ( ) { long long int a [ ] = { 4 , 1 , 8 , 7 } ; long long int b [ ] = { 2 , 3 , 6 , 5 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printf ( " % lld STRNEWLINE " , findMinSum ( a , b , n ) ) ; return 0 ; }