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

Given three sorted arrays A, B, and C of different sizes, we need to find the minimum absolute difference between the maximum and minimum values of any triplet (A[i], B[j], C[k]) where each element comes from a different array.

The key insight is to use a greedy approach: start from the largest elements of each array and progressively reduce the maximum element to minimize the difference.

Algorithm Steps

The algorithm works as follows ?

  • Start with pointers at the end of each array (largest elements)
  • Calculate the current difference between max and min of the triplet
  • Move the pointer of the array containing the maximum element backward
  • Continue until any pointer reaches the beginning
  • Track the minimum difference found

Implementation

def solve(A, B, C):
    i = len(A) - 1
    j = len(B) - 1
    k = len(C) - 1
    
    minimum_difference = 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]))
        
        if current_diff < minimum_difference:
            minimum_difference = current_diff
        
        maximum_term = max(A[i], B[j], C[k])
        
        if A[i] == maximum_term:
            i -= 1
        elif B[j] == maximum_term:
            j -= 1
        else:
            k -= 1
    
    return minimum_difference

# Example usage
A = [2, 5, 6, 9, 11]
B = [7, 10, 16]
C = [3, 4, 7, 7]

result = solve(A, B, C)
print(f"Minimum difference: {result}")
Minimum difference: 1

Step-by-Step Trace

Let's trace through the example to understand how it works ?

def solve_with_trace(A, B, C):
    i = len(A) - 1
    j = len(B) - 1
    k = len(C) - 1
    
    minimum_difference = abs(max(A[i], B[j], C[k]) - min(A[i], B[j], C[k]))
    print(f"Initial: A[{i}]={A[i]}, B[{j}]={B[j]}, C[{k}]={C[k]}")
    print(f"Initial difference: {minimum_difference}")
    
    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]))
        
        if current_diff < minimum_difference:
            minimum_difference = current_diff
            print(f"New minimum: {minimum_difference}")
        
        maximum_term = max(A[i], B[j], C[k])
        
        if A[i] == maximum_term:
            i -= 1
            print(f"Moving A pointer: i={i}")
        elif B[j] == maximum_term:
            j -= 1
            print(f"Moving B pointer: j={j}")
        else:
            k -= 1
            print(f"Moving C pointer: k={k}")
    
    return minimum_difference

A = [2, 5, 6, 9, 11]
B = [7, 10, 16]
C = [3, 4, 7, 7]

result = solve_with_trace(A, B, C)
print(f"Final result: {result}")
Initial: A[4]=11, B[2]=16, C[3]=7
Initial difference: 9
Moving B pointer: j=1
Moving A pointer: i=3
New minimum: 3
Moving B pointer: j=0
New minimum: 1
Moving A pointer: i=2
Moving A pointer: i=1
Moving A pointer: i=0
Final result: 1

Key Points

  • The algorithm has O(n + m + p) time complexity where n, m, p are array sizes
  • Space complexity is O(1) as we only use pointers
  • We always move the pointer with the maximum element to potentially reduce the difference
  • The optimal triplet in this example is (6, 7, 7) with difference 1

Conclusion

This greedy approach efficiently finds the minimum difference by systematically reducing the maximum element in each iteration. The algorithm terminates when any array is exhausted, ensuring we explore all relevant combinations.

Updated on: 2026-03-25T09:57:23+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements