Skip to content
Related Articles

Related Articles

Minimum sum of absolute differences of pairs in a triplet from three arrays
  • Last Updated : 06 Apr, 2021
GeeksforGeeks - Summer Carnival Banner

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;
}
Output:
3

Time Complexity: O(A*log A + C*log C + B)
Auxiliary Space: O(A + B + C)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :