Given two arrays a[] and b[] of equal length n. The task is to pair each element of array a to an element in array b, such that sum S of absolute differences of all the pairs is minimum.
Suppose, two elements a[i] and a[j] (i != j) of a are paired with elements b[p] and b[q] of b respectively,
then p should not be equal to q.
Examples:
Input : a[] = {3, 2, 1}
b[] = {2, 1, 3}
Output : 0
Explanation :
1st pairing: |3 - 2| + |2 - 1| + |1 - 3|
= 1 + 1 + 2 = 4
2nd pairing: |3 - 2| + |1 - 1| + |2 - 3|
= 1 + 0 + 1 = 2
3rd pairing: |2 - 2| + |3 - 1| + |1 - 3|
= 0 + 2 + 2 = 4
4th pairing: |1 - 2| + |2 - 1| + |3 - 3|
= 1 + 1 + 0 = 2
5th pairing: |2 - 2| + |1 - 1| + |3 - 3|
= 0 + 0 + 0 = 0
6th pairing: |1 - 2| + |3 - 1| + |2 - 3|
= 1 + 2 + 1 = 4
Therefore, 5th pairing has minimum sum of
absolute difference.
Input : n = 4
a[] = {4, 1, 8, 7}
b[] = {2, 3, 6, 5}
Output : 6
The solution to the problem is a simple greedy approach. It consists of two steps.
- Step 1 : Sort both the arrays in O (n log n) time.
- Step 2 : Find absolute difference of each pair of corresponding elements (elements at same index) of both arrays and add the result to the sum S. The time complexity of this step is O(n).
Hence, the overall time complexity of the program is O(n log n).
C++
#include <bits/stdc++.h>
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\n" , findMinSum(a, b, n));
return 0;
}
|
C
#include <stdio.h>
#include<stdlib.h>
void merge( int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort( int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int findMinSum( int a[], int b[], int n)
{
mergeSort(a,0,n-1);
mergeSort(b,0,n-1);
int sum= 0 ;
for ( int i=0; i<n; i++)
sum = sum + abs (a[i]-b[i]);
return sum;
}
int main()
{
int a[] = {4, 1, 8, 7};
int b[] = {2, 3, 6, 5};
int n = sizeof (a)/ sizeof (a[0]);
printf ( "%d\n" , findMinSum(a, b, n));
return 0;
}
|
Java
import java.util.Arrays;
class MinSum
{
static long findMinSum( long a[], long b[], long n)
{
Arrays.sort(a);
Arrays.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;
System.out.println(findMinSum(a, b, n));
}
}
|
Python3
def findMinSum(a, b, n):
a.sort()
b.sort()
sum = 0
for i in range (n):
sum = sum + abs (a[i] - b[i])
return sum
a = [ 4 , 1 , 8 , 7 ]
b = [ 2 , 3 , 6 , 5 ]
n = len (a)
print (findMinSum(a, b, n))
|
C#
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));
}
}
|
PHP
<?php
function findMinSum( $a , $b , $n )
{
sort( $a );
sort( $a , $n );
sort( $b );
sort( $b , $n );
$sum = 0 ;
for ( $i = 0; $i < $n ; $i ++)
$sum = $sum + abs ( $a [ $i ] -
$b [ $i ]);
return $sum ;
}
$a = array (4, 1, 8, 7);
$b = array (2, 3, 6, 5);
$n = sizeof( $a );
echo (findMinSum( $a , $b , $n ));
?>
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(n * logn)
Auxiliary Space: O(1)
This article is contributed by Sahil Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.