Program to find largest distance pair from two list of numbers in Python

We need to find the maximum value of the expression |a[i] - a[j]| + |b[i] - b[j]| + |i - j| for all valid pairs (i, j) where 0 ? i

The key insight is to transform the absolute value expression using the mathematical property that |x| + |y| = max(x+y, x-y, -x+y, -x-y). This allows us to convert the problem into finding maximum and minimum values efficiently.

Understanding the Problem

Given two lists A and B, we want to maximize:

  • |a[i] - a[j]| + |b[i] - b[j]| + |i - j|
  • Since i
  • So we need: |a[i] - a[j]| + |b[i] - b[j]| + (j - i)

Algorithm Approach

We transform the expression using four sign combinations (s, t) ? {(-1,-1), (-1,1), (1,-1), (1,1)} and compute s×a[i] + t×b[i] + i for each index. Then we find the maximum difference between any two such values.

Example

def find_largest_distance(a, b):
    ans = 0
    n = len(a)
    
    # Try all four sign combinations
    for s, t in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
        cur_min = float("inf")
        cur_max = float("-inf")
        
        for i in range(n):
            tmp = s * a[i] + t * b[i] + i
            cur_min = min(cur_min, tmp)
            cur_max = max(cur_max, tmp)
        
        ans = max(ans, cur_max - cur_min)
    
    return ans

# Test the function
A = [2, 4, 10, 6]
B = [3, 4, 7, 5]
result = find_largest_distance(A, B)
print(f"Maximum distance: {result}")
Maximum distance: 14

Step-by-Step Calculation

Let's trace through the example to verify the result:

A = [2, 4, 10, 6]
B = [3, 4, 7, 5]

# Manual calculation for i=0, j=2
i, j = 0, 2
distance = abs(A[i] - A[j]) + abs(B[i] - B[j]) + abs(i - j)
print(f"For i={i}, j={j}:")
print(f"|{A[i]} - {A[j]}| + |{B[i]} - {B[j]}| + |{i} - {j}|")
print(f"= {abs(A[i] - A[j])} + {abs(B[i] - B[j])} + {abs(i - j)}")
print(f"= {distance}")
For i=0, j=2:
|2 - 10| + |3 - 7| + |0 - 2|
= 8 + 4 + 2
= 14

How the Algorithm Works

The algorithm uses the mathematical transformation:

  • |a[i] - a[j]| + |b[i] - b[j]| can be expressed as max over four combinations
  • Each combination corresponds to: (s×a[i] + t×b[i] + i) - (s×a[j] + t×b[j] + j)
  • We find min and max values for each combination and take the maximum difference

Complete Solution with Validation

def find_largest_distance_with_validation(a, b):
    if len(a) != len(b):
        raise ValueError("Lists must have equal length")
    
    if len(a) < 2:
        return 0
    
    ans = 0
    n = len(a)
    
    for s, t in [(-1, -1), (-1, 1), (1, -1), (1, 1)]:
        cur_min = float("inf")
        cur_max = float("-inf")
        
        for i in range(n):
            tmp = s * a[i] + t * b[i] + i
            cur_min = min(cur_min, tmp)
            cur_max = max(cur_max, tmp)
        
        ans = max(ans, cur_max - cur_min)
    
    return ans

# Test cases
test_cases = [
    ([2, 4, 10, 6], [3, 4, 7, 5]),
    ([1, 2, 3], [4, 5, 6]),
    ([0, 0], [0, 0])
]

for i, (A, B) in enumerate(test_cases):
    result = find_largest_distance_with_validation(A, B)
    print(f"Test {i+1}: A={A}, B={B} ? Maximum distance: {result}")
Test 1: A=[2, 4, 10, 6], B=[3, 4, 7, 5] ? Maximum distance: 14
Test 2: A=[1, 2, 3], B=[4, 5, 6] ? Maximum distance: 4
Test 3: A=[0, 0], B=[0, 0] ? Maximum distance: 1

Conclusion

This algorithm efficiently finds the largest distance pair using mathematical transformation of absolute values. The time complexity is O(n) and space complexity is O(1), making it optimal for large datasets.

Updated on: 2026-03-25T12:30:44+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements