Program to find minimum difference between two elements from two lists in Python

Sometimes we need to find the smallest possible difference between any element from one list and any element from another list. Python provides an efficient solution using the two-pointer technique after sorting both lists.

So, if the input is like L1 = [2, 7, 4], L2 = [16, 10, 11], then the output will be 3, as the smallest difference is 10 - 7 = 3.

Algorithm Steps

To solve this efficiently, we will follow these steps ?

  • Sort both lists L1 and L2
  • Initialize minimum difference as infinity
  • Use two pointers: i = 0 for L1, j = 0 for L2
  • While both pointers are within bounds:
    • Calculate current difference and update minimum
    • Move the pointer of the smaller element forward
  • Return the minimum difference found

Using Two-Pointer Technique

The most efficient approach uses sorting and two pointers ?

def find_min_difference(L1, L2):
    L1.sort()
    L2.sort()
    
    min_diff = float("inf")
    i = j = 0
    
    while i < len(L1) and j < len(L2):
        min_diff = min(min_diff, abs(L1[i] - L2[j]))
        
        if L1[i] < L2[j]:
            i += 1
        else:
            j += 1
    
    return min_diff

# Test with example
L1 = [2, 7, 4]
L2 = [16, 10, 11]
result = find_min_difference(L1, L2)
print(f"Minimum difference: {result}")
Minimum difference: 3

How It Works

After sorting: L1 = [2, 4, 7] and L2 = [10, 11, 16]

def find_min_difference_detailed(L1, L2):
    L1_sorted = sorted(L1)
    L2_sorted = sorted(L2)
    
    print(f"Sorted L1: {L1_sorted}")
    print(f"Sorted L2: {L2_sorted}")
    
    min_diff = float("inf")
    i = j = 0
    
    while i < len(L1_sorted) and j < len(L2_sorted):
        current_diff = abs(L1_sorted[i] - L2_sorted[j])
        print(f"Comparing {L1_sorted[i]} and {L2_sorted[j]}: diff = {current_diff}")
        
        min_diff = min(min_diff, current_diff)
        
        if L1_sorted[i] < L2_sorted[j]:
            i += 1
        else:
            j += 1
    
    return min_diff

# Test with example
L1 = [2, 7, 4]
L2 = [16, 10, 11]
result = find_min_difference_detailed(L1, L2)
print(f"\nMinimum difference: {result}")
Sorted L1: [2, 4, 7]
Sorted L2: [10, 11, 16]
Comparing 2 and 10: diff = 8
Comparing 4 and 10: diff = 6
Comparing 7 and 10: diff = 3
Comparing 7 and 11: diff = 4

Minimum difference: 3

Alternative Approach: Brute Force

For comparison, here's the brute force method that checks all pairs ?

def find_min_difference_brute_force(L1, L2):
    min_diff = float("inf")
    
    for num1 in L1:
        for num2 in L2:
            current_diff = abs(num1 - num2)
            min_diff = min(min_diff, current_diff)
    
    return min_diff

# Test with example
L1 = [2, 7, 4]
L2 = [16, 10, 11]
result = find_min_difference_brute_force(L1, L2)
print(f"Minimum difference (brute force): {result}")
Minimum difference (brute force): 3

Time Complexity Comparison

Method Time Complexity Space Complexity
Two-Pointer (after sorting) O(n log n + m log m) O(1)
Brute Force O(n × m) O(1)

Conclusion

The two-pointer technique after sorting provides an efficient O(n log n + m log m) solution for finding minimum difference between elements from two lists. This approach is significantly better than the O(n × m) brute force method for large datasets.

Updated on: 2026-03-25T11:26:34+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements