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++
// C++ program for the above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Function to find the value
// closest to K in the array A[]
int closestValue(vector<int> A, int k)
{
// Initialize close value as the
// end element
int close = A.back();
// Find lower bound of the array
auto it = lower_bound(A.begin(),
A.end(), k);
// If lower_bound is found
if (it != A.end()) {
close = *it;
// If lower_bound is not
// the first array element
if (it != A.begin()) {
// If *(it - 1) is closer to k
if ((k - *(it - 1))
< (close - k)) {
close = *(it - 1);
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
void minPossible(vector<int> arr,
vector<int> brr,
vector<int> crr)
{
// Sort the vectors arr and crr
sort(arr.begin(), arr.end());
sort(crr.begin(), crr.end());
// Initialize minimum as INT_MAX
int minimum = INT_MAX;
// Traverse the array brr[]
for (int val : brr) {
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (abs(val - arr_close)
+ abs(val - crr_close)
< minimum)
// Update the minimum
minimum = abs(val - arr_close)
+ abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
cout << minimum;
}
// Driver Code
int main()
{
vector<int> a = { 1, 8, 5 };
vector<int> b = { 2, 9 };
vector<int> c = { 5, 4 };
// Function Call
minPossible(a, b, c);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Lower_bound function
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;
}
// Function to find the value
// closest to K in the array A[]
static int closestValue(int A[], int k)
{
// Initialize close value as the
// end element
int close = A[A.length - 1];
// Find lower bound of the array
int it = lower_bound(A, k);
// If lower_bound is found
if (it != A.length)
{
close = A[it];
// If lower_bound is not
// the first array element
if (it != 0)
{
// If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
static void minPossible(int arr[], int brr[],
int crr[])
{
// Sort the vectors arr and crr
Arrays.sort(arr);
Arrays.sort(crr);
// Initialize minimum as INT_MAX
int minimum = Integer.MAX_VALUE;
// Traverse the array brr[]
for(int val : brr)
{
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (Math.abs(val - arr_close) +
Math.abs(val - crr_close) < minimum)
// Update the minimum
minimum = Math.abs(val - arr_close) +
Math.abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
System.out.println(minimum);
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 8, 5 };
int b[] = { 2, 9 };
int c[] = { 5, 4 };
// Function Call
minPossible(a, b, c);
}
}
// This code is contributed by Kingash
Python3
# Python program to implement
# the above approach
# Lower_bound function
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;
# Function to find the value
# closest to K in the array A[]
def closestValue(A, k):
# Initialize close value as the
# end element
close = A[-1];
# Find lower bound of the array
it = lower_bound(A, k);
# If lower_bound is found
if (it != len(A)):
close = A[it];
# If lower_bound is not
# the first array element
if (it != 0):
# If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k)):
close = A[it - 1];
# Return closest value of k
return close;
# Function to find the minimum sum of
# abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
def minPossible(arr, brr, crr):
# Sort the vectors arr and crr
arr.sort();
crr.sort();
# Initialize minimum as LET_MAX
minimum = 10**9;
# Traverse the array brr[]
for val in brr:
# Stores the element closest
# to val from the array arr[]
arr_close = closestValue(arr, val);
# Stores the element closest
# to val from the array crr[]
crr_close = closestValue(crr, val);
# If sum of differences is minimum
if (abs(val - arr_close) +
abs(val - crr_close) < minimum):
# Update the minimum
minimum = abs(val - arr_close) + abs(val - crr_close);
# Print the minimum absolute
# difference possible
print(minimum);
# Driver code
a = [ 1, 8, 5 ];
b = [ 2, 9 ];
c = [ 5, 4 ];
# Function Call
minPossible(a, b, c);
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
// Lower_bound function
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;
}
// Function to find the value
// closest to K in the array A[]
static int closestValue(int []A, int k)
{
// Initialize close value as the
// end element
int close = A[A.Length - 1];
// Find lower bound of the array
int it = lower_bound(A, k);
// If lower_bound is found
if (it != A.Length)
{
close = A[it];
// If lower_bound is not
// the first array element
if (it != 0)
{
// If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
static void minPossible(int[] arr, int[] brr,
int[] crr)
{
// Sort the vectors arr and crr
Array.Sort(arr);
Array.Sort(crr);
// Initialize minimum as INT_MAX
int minimum = Int32.MaxValue;
// Traverse the array brr[]
foreach(int val in brr)
{
// Stores the element closest
// to val from the array arr[]
int arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
int crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (Math.Abs(val - arr_close) +
Math.Abs(val - crr_close) < minimum)
// Update the minimum
minimum = Math.Abs(val - arr_close) +
Math.Abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
Console.WriteLine(minimum);
}
// Driver Code
static void Main()
{
int []a = { 1, 8, 5 };
int []b = { 2, 9 };
int []c = { 5, 4 };
// Function Call
minPossible(a, b, c);
}
}
// This code is contributed by SoumikMondal
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Lower_bound function
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 to find the value
// closest to K in the array A[]
function closestValue(A, k)
{
// Initialize close value as the
// end element
let close = A[A.length - 1];
// Find lower bound of the array
let it = lower_bound(A, k);
// If lower_bound is found
if (it != A.length)
{
close = A[it];
// If lower_bound is not
// the first array element
if (it != 0)
{
// If *(it - 1) is closer to k
if ((k - A[it - 1]) < (close - k))
{
close = A[it - 1];
}
}
}
// Return closest value of k
return close;
}
// Function to find the minimum sum of
// abs(arr[i] - brr[j]) and abs(brr[j]-crr[k])
function minPossible(arr, brr, crr)
{
// Sort the vectors arr and crr
arr.sort();
crr.sort();
// Initialize minimum as LET_MAX
let minimum = Number.MAX_VALUE;
// Traverse the array brr[]
for(let val in brr)
{
// Stores the element closest
// to val from the array arr[]
let arr_close = closestValue(arr, val);
// Stores the element closest
// to val from the array crr[]
let crr_close = closestValue(crr, val);
// If sum of differences is minimum
if (Math.abs(val - arr_close) +
Math.abs(val - crr_close) < minimum)
// Update the minimum
minimum = Math.abs(val - arr_close) +
Math.abs(val - crr_close);
}
// Print the minimum absolute
// difference possible
document.write(minimum);
}
// Driver code
let a = [ 1, 8, 5 ];
let b = [ 2, 9 ];
let c = [ 5, 4 ];
// Function Call
minPossible(a, b, c);
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(A*log A + C*log C + B*log A + B*log C)
Auxiliary Space: O(A + B + C)
Similar Reads
Minimum sum of absolute differences between pairs of a triplet from an array Given an array A[] consisting of positive integers, the task is to find the minimum value of |A[x] - A[y]| + |A[y] - A[z]| of any triplet (A[x], A[y], A[z]) from an array. Examples: Input: A[] = { 1, 1, 2, 3 }Output: 1Explanation:For x = 0, y = 1, z = 2|A[x] - A[y]| + |A[y] - A[z]| = 0 + 1 = 1, whic
5 min read
Minimum possible sum of absolute difference of pairs from given arrays Given two arrays a[] and b[] of size N and M respectively (N < M), the task is to find the minimum possible sum of absolute difference of pairs formed by pairing each element of array a[] with an element of array b[] Note: Each element of each array can be considered only once. Examples: Input: a
8 min read
Minimum sum of absolute difference of pairs of two arrays 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
7 min read
Maximum sum of absolute differences between distinct pairs of a triplet from an array Given an array arr[] consisting of N integers, the task is to find the maximum sum of absolute differences between all distinct pairs of the triplet in the array. Examples: Input: arr[] = {1, 2, 3, 4}Output: 6Explanation:The valid triplet is (1, 3, 4) as sum = |1 - 4| + |1 - 3| + |3 - 4| = 6, which
4 min read
Sum of absolute differences of all pairs in a given array Given a sorted array of distinct elements, the task is to find the summation of absolute differences of all pairs in the given array. Examples: Input : arr[] = {1, 2, 3, 4} Output: 10 Sum of |2-1| + |3-1| + |4-1| + |3-2| + |4-2| + |4-3| = 10 Input : arr[] = {1, 8, 9, 15, 16} Output: 74 Input : arr[]
11 min read
Minimum and Maximum sum of absolute differences of pairs Given an array of N integers where N is even, find the minimum and maximum sum of absolute difference of N/2 pairs formed by pairing every element with one other element. Examples: Input: a[] = {10, -10, 20, -40} Output: min_sum = 40, max_sum = 80 Explanation: Pairs selected for minimum sum (-10, -4
8 min read
Array element with minimum sum of absolute differences | Set 2 Given an array arr[] consisting of N positive integers, the task is to find an array element X such that sum of its absolute differences with every array element is minimum. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Explanation: For element arr[0](= 1): |(1 - 1)| + |(2 - 1)| + |(3 - 1)| + |(4
7 min read
Minimum sum of all differences between unique pairs in the Array Given an array arr[] consisting of N integers, the task is to find the minimum sum of all absolute differences between unique pairs of elements in the array after updating the array arr[]. To update the array, any two elements from the array can be chosen in any order. 1 is subtracted from the first
7 min read
Find maximum absolute difference with min sum of ratios in an Array Given an array of positive integers arr[], the task is to find the maximum absolute difference between the pairs from the array such that the sum of their ratios is minimum. Examples: Input: arr[] = {2, 6, 3, 4, 8, 9}Output: 4Explanation: In the above example, the ratios of every pair are calculated
7 min read
Sum of minimum absolute differences in an array Given an array of n distinct integers. The task is to find the sum of minimum absolute difference of each array element. For an element arr[i] present at index i in the array, its minimum absolute difference is calculated as: Min absolute difference (arr[i]) = min(abs(arr[i] - arr[j])), where 0 <
7 min read