Open In App

Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays

Improve
Improve
Like Article
Like
Save
Share
Report

Given three sorted arrays A, B, and C of not necessarily same sizes. Calculate the minimum absolute difference between the maximum and minimum number of any triplet A[i], B[j], C[k] such that they belong to arrays A, B and C respectively, i.e., minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k]))

Examples: 

Input : A : [ 1, 4, 5, 8, 10 ]
        B : [ 6, 9, 15 ]
        C : [ 2, 3, 6, 6 ]
Output : 1
Explanation: When we select A[i] = 5
B[j] = 6, C[k] = 6, we get the minimum difference 
as max(A[i], B[j], C[k]) - min(A[i], B[j], C[k]))
= |6-5| = 1 

Input : A = [ 5, 8, 10, 15 ]
        B = [ 6, 9, 15, 78, 89 ]
        C = [ 2, 3, 6, 6, 8, 8, 10 ]
Output : 1
Explanation: When we select A[i] = 10
b[j] = 9, C[k] = 10.

Start with the largest elements in each of the arrays A, B & C. Maintain a variable to update the answer during each of the steps to be followed. 
In every step, the only possible way to decrease the difference is to decrease the maximum element out of the three elements. 
So traverse to the next largest element in the array containing the maximum element for this step and update the answer variable. 
Repeat this step until the array containing the maximum element ends. 

Implementation:

C++




// C++ code for above approach
#include<bits/stdc++.h>
using namespace std;
 
int solve(int A[], int B[], int C[], int i, int j, int k)
{
        int min_diff, current_diff, max_term;
 
        // calculating min difference from last
        // index of lists
           min_diff = abs(max(A[i], max(B[j], C[k]))
                            - min(A[i], min(B[j], C[k])));
 
        while (i != -1 && j != -1 && k != -1)
        {
            current_diff = abs(max(A[i], max(B[j], C[k]))
                            - min(A[i], min(B[j], C[k])));
 
            // checking condition
            if (current_diff < min_diff)
                min_diff = current_diff;
 
            // calculating max term from list
            max_term = max(A[i], max(B[j], C[k]));
 
            // Moving to smaller value in the
            // array with maximum out of three.
            if (A[i] == max_term)
                i -= 1;
            else if (B[j] == max_term)
                j -= 1;
            else
                k -= 1;
        }
         
        return min_diff;
    }
 
    // Driver code
    int main()
    {
        int D[] = { 5, 8, 10, 15 };
        int E[] = { 6, 9, 15, 78, 89 };
        int F[] = { 2, 3, 6, 6, 8, 8, 10 };
        int nD = sizeof(D) / sizeof(D[0]);
        int nE = sizeof(E) / sizeof(E[0]);
        int nF = sizeof(F) / sizeof(F[0]);
        cout << solve(D, E, F, nD-1, nE-1, nF-1);
         
        return 0;
    }
 
// This code is contributed by Ravi Maurya.


Java




// Java code for above approach
import java.util.*;
 
class GFG
{
    static int solve(int[] A, int[] B, int[] C)
    {
        int i, j, k;
         
        // assigning the length -1 value
        // to each of three variables
        i = A.length - 1;
        j = B.length - 1;
        k = C.length - 1;
         
        int min_diff, current_diff, max_term;
 
        // calculating min difference
        // from last index of lists
        min_diff = Math.abs(Math.max(A[i], Math.max(B[j], C[k]))
                - Math.min(A[i], Math.min(B[j], C[k])));
 
        while (i != -1 && j != -1 && k != -1)
        {
            current_diff = Math.abs(Math.max(A[i], Math.max(B[j], C[k]))
                        - Math.min(A[i], Math.min(B[j], C[k])));
 
            // checking condition
            if (current_diff < min_diff)
                min_diff = current_diff;
 
            // calculating max term from list
            max_term = Math.max(A[i], Math.max(B[j], C[k]));
 
            // Moving to smaller value in the
            // array with maximum out of three.
            if (A[i] == max_term)
                i -= 1;
            else if (B[j] == max_term)
                j -= 1;
            else
                k -= 1;
        }
        return min_diff;
    }
 
    // Driver code
    public static void main(String []args)
    {
    
        int[] D = { 5, 8, 10, 15 };
        int[] E = { 6, 9, 15, 78, 89 };
        int[] F = { 2, 3, 6, 6, 8, 8, 10 };
        System.out.println(solve(D, E, F));
         
    }
}
 
// This code is contributed by rutvik_56.


Python3




# python code for above approach.
 
def solve(A, B, C):
 
        # assigning the length -1 value
        # to each of three variables
        i = len(A) - 1
        j = len(B) - 1
        k = len(C) - 1
 
        # calculating min difference
        # from last index of lists
        min_diff = abs(max(A[i], B[j], C[k]) -
        min(A[i], B[j], C[k]))
 
        while i != -1 and j != -1 and k != -1:
            current_diff = abs(max(A[i], B[j],
            C[k]) - min(A[i], B[j], C[k]))
 
            # checking condition
            if current_diff < min_diff:
                min_diff = current_diff
 
            # calculating max term from list
            max_term = max(A[i], B[j], C[k])
 
            # Moving to smaller value in the
            # array with maximum out of three.
            if A[i] == max_term:
                i -= 1
            elif B[j] == max_term:
                j -= 1
            else:
                k -= 1
        return min_diff
 
# driver code
 
A = [ 5, 8, 10, 15 ]
B = [ 6, 9, 15, 78, 89 ]
C = [ 2, 3, 6, 6, 8, 8, 10 ]
print(solve(A, B, C))


C#




// C# code for above approach
using System;
 
class GFG
{
    static int solve(int[] A, int[] B, int[] C)
    {
        int i, j, k;
         
        // assigning the length -1 value
        // to each of three variables
        i = A.Length - 1;
        j = B.Length - 1;
        k = C.Length - 1;
         
        int min_diff, current_diff, max_term;
 
        // calculating min difference
        // from last index of lists
        min_diff = Math.Abs(Math.Max(A[i], Math.Max(B[j], C[k]))
                - Math.Min(A[i], Math.Min(B[j], C[k])));
 
        while (i != -1 && j != -1 && k != -1)
        {
            current_diff = Math.Abs(Math.Max(A[i], Math.Max(B[j], C[k]))
                        - Math.Min(A[i], Math.Min(B[j], C[k])));
 
            // checking condition
            if (current_diff < min_diff)
                min_diff = current_diff;
 
            // calculating max term from list
            max_term = Math.Max(A[i], Math.Max(B[j], C[k]));
 
            // Moving to smaller value in the
            // array with maximum out of three.
            if (A[i] == max_term)
                i -= 1;
            else if (B[j] == max_term)
                j -= 1;
            else
                k -= 1;
        }
        return min_diff;
    }
 
    // Driver code
    public static void Main()
    {
    
        int[] D = { 5, 8, 10, 15 };
        int[] E = { 6, 9, 15, 78, 89 };
        int[] F = { 2, 3, 6, 6, 8, 8, 10 };
        Console.WriteLine(solve(D, E, F));
         
    }
}
// This code is contributed by vt_m


PHP




<?php
// PHP code for above approach
function solve($A, $B, $C,
               $i, $j, $k)
{
    $min_diff;
    $current_diff;
    $max_term;
 
    // calculating min difference
    // from last index of lists
    $min_diff = abs(max($A[$i], max($B[$j], $C[$k])) -
                    min($A[$i], min($B[$j], $C[$k])));
 
    while ($i != -1 &&
           $j != -1 && $k != -1)
    {
        $current_diff = abs(max($A[$i], max($B[$j], $C[$k])) -
                            min($A[$i], min($B[$j], $C[$k])));
 
        // checking condition
        if ($current_diff < $min_diff)
            $min_diff = $current_diff;
 
        // calculating max term from list
        $max_term = max($A[$i],
                    max($B[$j], $C[$k]));
 
        // Moving to smaller value in the
        // array with maximum out of three.
        if ($A[$i] == $max_term)
            $i -= 1;
        else if ($B[$j] == $max_term)
            $j -= 1;
        else
            $k -= 1;
    }
     
    return $min_diff;
}
 
// Driver code
 
$D = array (5, 8, 10, 15);
$E = array (6, 9, 15, 78, 89);
$F = array (2, 3, 6, 6, 8, 8, 10);
 
$nD = sizeof($D) ;
$nE = sizeof($E) ;
$nF = sizeof($F);
echo solve($D, $E, $F,
           $nD - 1, $nE - 1,
           $nF - 1);
 
// This code is contributed by ajit
?>


Javascript




<script>
 
// javascript program for above approach
 
function solve( A, B,C)
    {
        let i, j, k;
           
        // assigning the length -1 value
        // to each of three variables
        i = A.length - 1;
        j = B.length - 1;
        k = C.length - 1;
           
        let min_diff, current_diff, max_term;
   
        // calculating min difference
        // from last index of lists
        min_diff = Math.abs(Math.max(A[i], Math.max(B[j], C[k]))
                  - Math.min(A[i], Math.min(B[j], C[k])));
   
        while (i != -1 && j != -1 && k != -1)
        {
            current_diff = Math.abs(Math.max(A[i], Math.max(B[j], C[k]))
                          - Math.min(A[i], Math.min(B[j], C[k])));
   
            // checking condition
            if (current_diff < min_diff)
                min_diff = current_diff;
   
            // calculating max term from list
            max_term = Math.max(A[i], Math.max(B[j], C[k]));
   
            // Moving to smaller value in the
            // array with maximum out of three.
            if (A[i] == max_term)
                i -= 1;
            else if (B[j] == max_term)
                j -= 1;
            else
                k -= 1;
        }
        return min_diff;
    }
 
// Driver Function
         let D = [ 5, 8, 10, 15 ];
         let E = [ 6, 9, 15, 78, 89 ];
         let F = [ 2, 3, 6, 6, 8, 8, 10 ];
        document.write(solve(D, E, F));
            
        // This code is contributed by susmitakundugoaldnga.
</script>


Output

1

Time Complexity : O(n), where n is the combined sizes of all input arrays.
Auxiliary Space: O(1), since no extra space has been taken



Last Updated : 21 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads