Minimum sum of absolute differences of pairs in a triplet from three arrays
Last Updated :
17 Aug, 2023
Given three arrays a[], b[] and c[] of sizes A, B and C respectively, the task is to find the minimum possible value of abs(a[i] – b[j]) + abs(b[j] – c[k]) where 0 ? i ? A, 0 ? j ? B and 0 ? k ? C.
Examples:
Input: A = 3, B = 2, C = 2, a[] = {1, 8, 5}, b[] = {2, 9}, c[] = {5, 4}
Output: 3
Explanation:
The triplet (a[0], b[0], c[1]), i.e. (1, 2, 4) has minimum sum of absolute difference of pairs, i.e. abs(1 – 2) + abs(2 – 4) = 1 + 2 = 3.
Input: A = 4, B = 3, C = 3, a[] = {4, 5, 1, 7}, b[] = {8, 5, 6}, c[] = {2, 7, 12}
Output: 2
Explanation:
The triplet (a[1], b[1], c[1]), i.e. (1, 5, 7) has minimum sum of absolute difference of pairs, i.e. abs(5 – 5) + abs(5 – 7) = 0 + 2 = 2.
Approach: The idea to solve this problem is to sort the arrays a[] and c[] and then traverse the array b[] and find the elements which satisfy the given condition.
Follow the steps below to solve the problem:
- Initialize the variable, say min, as INT_MAX, to store the minimum possible value.
- Sort the arrays a[] and c[] in increasing order.
- Traverse the array b[] and for each element, say b[i], find the closest element to b[i] from the arrays a[] and c[] as arr_close and crr_close and do the following:
- To find the closest element, firstly find the lower_bound of the target element b[i].
- If the lower bound is found, check if it is the first element of the array or not. If it is not, then compare the lower bound and its previous element with the target element and find which is closest to the target element.
- If the lower bound is not found, then the closest element will be the last element of the array.
- Update min as the minimum of abs(b[i] – arr_close) + abs(b[i] – crr_close).
- After completing the above steps, print the value of min as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int closestValue(vector< int > A, int k)
{
int close = A.back();
auto it = lower_bound(A.begin(),
A.end(), k);
if (it != A.end()) {
close = *it;
if (it != A.begin()) {
if ((k - *(it - 1))
< (close - k)) {
close = *(it - 1);
}
}
}
return close;
}
void minPossible(vector< int > arr,
vector< int > brr,
vector< int > crr)
{
sort(arr.begin(), arr.end());
sort(crr.begin(), crr.end());
int minimum = INT_MAX;
for ( int val : brr) {
int arr_close = closestValue(arr, val);
int crr_close = closestValue(crr, val);
if ( abs (val - arr_close)
+ abs (val - crr_close)
< minimum)
minimum = abs (val - arr_close)
+ abs (val - crr_close);
}
cout << minimum;
}
int main()
{
vector< int > a = { 1, 8, 5 };
vector< int > b = { 2, 9 };
vector< int > c = { 5, 4 };
minPossible(a, b, c);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
public static int lower_bound( int arr[], int key)
{
int low = 0 ;
int high = arr.length - 1 ;
while (low < high)
{
int mid = low + (high - low) / 2 ;
if (arr[mid] >= key)
{
high = mid;
}
else
{
low = mid + 1 ;
}
}
return low;
}
static int closestValue( int A[], int k)
{
int close = A[A.length - 1 ];
int it = lower_bound(A, k);
if (it != A.length)
{
close = A[it];
if (it != 0 )
{
if ((k - A[it - 1 ]) < (close - k))
{
close = A[it - 1 ];
}
}
}
return close;
}
static void minPossible( int arr[], int brr[],
int crr[])
{
Arrays.sort(arr);
Arrays.sort(crr);
int minimum = Integer.MAX_VALUE;
for ( int val : brr)
{
int arr_close = closestValue(arr, val);
int crr_close = closestValue(crr, val);
if (Math.abs(val - arr_close) +
Math.abs(val - crr_close) < minimum)
minimum = Math.abs(val - arr_close) +
Math.abs(val - crr_close);
}
System.out.println(minimum);
}
public static void main(String[] args)
{
int a[] = { 1 , 8 , 5 };
int b[] = { 2 , 9 };
int c[] = { 5 , 4 };
minPossible(a, b, c);
}
}
|
Python3
def lower_bound(arr, key):
low = 0 ;
high = len (arr) - 1 ;
while (low < high):
mid = low + (high - low) / / 2 ;
if (arr[mid] > = key):
high = mid;
else :
low = mid + 1 ;
return low;
def closestValue(A, k):
close = A[ - 1 ];
it = lower_bound(A, k);
if (it ! = len (A)):
close = A[it];
if (it ! = 0 ):
if ((k - A[it - 1 ]) < (close - k)):
close = A[it - 1 ];
return close;
def minPossible(arr, brr, crr):
arr.sort();
crr.sort();
minimum = 10 * * 9 ;
for val in brr:
arr_close = closestValue(arr, val);
crr_close = closestValue(crr, val);
if ( abs (val - arr_close) +
abs (val - crr_close) < minimum):
minimum = abs (val - arr_close) + abs (val - crr_close);
print (minimum);
a = [ 1 , 8 , 5 ];
b = [ 2 , 9 ];
c = [ 5 , 4 ];
minPossible(a, b, c);
|
C#
using System;
class GFG{
public static int lower_bound( int [] arr, int key)
{
int low = 0;
int high = arr.Length - 1;
while (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] >= key)
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
static int closestValue( int []A, int k)
{
int close = A[A.Length - 1];
int it = lower_bound(A, k);
if (it != A.Length)
{
close = A[it];
if (it != 0)
{
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
return close;
}
static void minPossible( int [] arr, int [] brr,
int [] crr)
{
Array.Sort(arr);
Array.Sort(crr);
int minimum = Int32.MaxValue;
foreach ( int val in brr)
{
int arr_close = closestValue(arr, val);
int crr_close = closestValue(crr, val);
if (Math.Abs(val - arr_close) +
Math.Abs(val - crr_close) < minimum)
minimum = Math.Abs(val - arr_close) +
Math.Abs(val - crr_close);
}
Console.WriteLine(minimum);
}
static void Main()
{
int []a = { 1, 8, 5 };
int []b = { 2, 9 };
int []c = { 5, 4 };
minPossible(a, b, c);
}
}
|
Javascript
<script>
function lower_bound(arr, key)
{
let low = 0;
let high = arr.length - 1;
while (low < high)
{
let mid = low + Math.floor((high - low) / 2);
if (arr[mid] >= key)
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
function closestValue(A, k)
{
let close = A[A.length - 1];
let it = lower_bound(A, k);
if (it != A.length)
{
close = A[it];
if (it != 0)
{
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
return close;
}
function minPossible(arr, brr, crr)
{
arr.sort();
crr.sort();
let minimum = Number.MAX_VALUE;
for (let val in brr)
{
let arr_close = closestValue(arr, val);
let crr_close = closestValue(crr, val);
if (Math.abs(val - arr_close) +
Math.abs(val - crr_close) < minimum)
minimum = Math.abs(val - arr_close) +
Math.abs(val - crr_close);
}
document.write(minimum);
}
let a = [ 1, 8, 5 ];
let b = [ 2, 9 ];
let c = [ 5, 4 ];
minPossible(a, b, c);
</script>
|
Time Complexity: O(A*log A + C*log C + B*log A + B*log C)
Auxiliary Space: O(A + B + C)
Share your thoughts in the comments
Please Login to comment...