Problem statement

https://binarysearch.com/problems/Largest-Distance-Pair/

Solution

Equal to Leetcode 1131. Maximum of Absolute Value Expression.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, A, B):
        f = lambda x, y, z: max(a*x + b*y + i*z for a, b, i in zip(A, B, range(len(A))))
        return max(f(a,b,1) + f(-a,-b,-1) for a, b in [(-1, -1), (-1, 1), (1, -1), (1, 1)])