TL;DR
Notes. For details, please refer to Recent 45-Degree Rotation Developments .
Manhattan Distance
The Manhattan distance between two points i i i and j j j is expressed as:
∣ x i − x j ∣ + ∣ y i − y j ∣ |x_i - x_j| + |y_i - y_j| ∣ x i − x j ∣ + ∣ y i − y j ∣
Maximum Manhattan Distance
Given m points, we want to find the maximum Manhattan distance between any pair of points.
A brute-force approach would take O ( m 2 ) O(m^2) O ( m 2 ) .
Instead, we use a technique called 45-degree rotation. A 45-degree rotation is the transformation ( x ′ , y ′ ) − > ( x − y , x + y ) (x', y') -> (x - y, x + y) ( x ′ , y ′ ) − > ( x − y , x + y ) . Originally, a factor of 2 \sqrt{2} 2 is applied to the magnitude, but when magnitude is not essential, it can be ignored. In other words, when you see x + y x+y x + y or x − y x-y x − y , you should suspect a 45-degree rotation might be applicable.
The Manhattan distance doesn't reveal this directly, but by applying the following transformation, x + y x+y x + y and x − y x-y x − y emerge.
First, consider the absolute value as follows:
∣ x ∣ = m a x ( x , − x ) |x| = max(x, -x) ∣ x ∣ = ma x ( x , − x )
Then the Manhattan distance becomes:
∣ x i − x j ∣ + ∣ y i − y j ∣ = m a x ( x i − x j , − x i + x j ) + m a x ( y i − y j , − y i + y j ) = m a x ( ( x i − x j ) + ( y i − y j ) , − ( x i − x j ) + ( y i − y j ) , ( x i − x j ) − ( y i − y j ) , − ( x i − x j ) + ( y i − y j ) ) = m a x ( ( x i + y i ) − ( x j + y j ) , − ( x i − y i ) + ( x j − y j ) , ( x i − y i ) − ( x j − y j ) , − ( x i + y i ) + ( x j + y j ) ) \begin{aligned}
|x_i - x_j| + |y_i - y_j| &= max(x_i - x_j, -x_i + x_j) + max(y_i - y_j, -y_i + y_j) \\
&= max((x_i - x_j) + (y_i - y_j), -(x_i - x_j) + (y_i - y_j), (x_i - x_j) - (y_i - y_j), -(x_i - x_j) + (y_i - y_j)) \\
&= max((x_i + y_i) - (x_j + y_j), -(x_i - y_i) + (x_j - y_j), (x_i - y_i) - (x_j - y_j), -(x_i + y_i) + (x_j + y_j))
\end{aligned} ∣ x i − x j ∣ + ∣ y i − y j ∣ = ma x ( x i − x j , − x i + x j ) + ma x ( y i − y j , − y i + y j ) = ma x (( x i − x j ) + ( y i − y j ) , − ( x i − x j ) + ( y i − y j ) , ( x i − x j ) − ( y i − y j ) , − ( x i − x j ) + ( y i − y j )) = ma x (( x i + y i ) − ( x j + y j ) , − ( x i − y i ) + ( x j − y j ) , ( x i − y i ) − ( x j − y j ) , − ( x i + y i ) + ( x j + y j ))
Looking at the 1st and 4th terms, and the 2nd and 3rd terms, they can be converted back to absolute values:
m a x ( ( ∣ x i + y i ) − ( x j + y j ) ∣ , ∣ ( x i − y i ) − ( x j − y j ) ∣ ) max((|x_i + y_i) - (x_j + y_j)|, |(x_i - y_i) - (x_j - y_j)|) ma x (( ∣ x i + y i ) − ( x j + y j ) ∣ , ∣ ( x i − y i ) − ( x j − y j ) ∣ )
This reveals x + y x + y x + y and x − y x - y x − y , so we can substitute x ′ = x + y x' = x + y x ′ = x + y and y ′ = x − y y' = x - y y ′ = x − y .
Therefore:
d i j = m a x ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ ) d_{ij} = max(|x_i' - x_j'|, |y_i' - y_j'|) d ij = ma x ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ )
Our original goal was to find:
m a x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( d i j ) max_{1\leq i\leq n,1\leq j \leq n}(d_{ij}) ma x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( d ij )
Transforming this gives:
m a x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( m a x ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ ) ) max_{1\leq i\leq n,1\leq j \leq n}(max(|x_i' - x_j'|, |y_i' - y_j'|)) ma x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( ma x ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ ))
The outer m a x 1 ≤ i ≤ n , 1 ≤ j ≤ n max_{1\leq i\leq n,1\leq j \leq n} ma x 1 ≤ i ≤ n , 1 ≤ j ≤ n and the inner m a x max ma x can be swapped without changing the result:
m a x ( m a x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ ) ) max(max_{1\leq i\leq n,1\leq j \leq n}(|x_i' - x_j'|, |y_i' - y_j'|)) ma x ( ma x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( ∣ x i ′ − x j ′ ∣ , ∣ y i ′ − y j ′ ∣ ))
Now, x ′ x' x ′ and y ′ y' y ′ can be computed independently, so we just need to subtract the smallest from the largest. Therefore:
m a x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( ∣ x i ′ − x j ′ ∣ ) = m a x 1 ≤ i ≤ n ( x i ′ ) − m i n 1 ≤ j ≤ n ( x j ′ ) max_{1\leq i\leq n,1\leq j \leq n}(|x_i' - x_j'|) = max_{1\leq i \leq n}(x_i') - min_{1\leq j \leq n}(x'_j) ma x 1 ≤ i ≤ n , 1 ≤ j ≤ n ( ∣ x i ′ − x j ′ ∣ ) = ma x 1 ≤ i ≤ n ( x i ′ ) − mi n 1 ≤ j ≤ n ( x j ′ )
Applying the same to the y ′ y' y ′ side:
m a x ( m a x 1 ≤ i ≤ n ( x i ′ ) − m i n 1 ≤ j ≤ n ( x j ′ ) , m a x 1 ≤ i ≤ n ( y i ′ ) − m i n 1 ≤ j ≤ n ( y j ′ ) ) max(max_{1\leq i \leq n}(x_i') - min_{1\leq j \leq n}(x'_j), max_{1\leq i \leq n}(y_i') - min_{1\leq j \leq n}(y'_j)) ma x ( ma x 1 ≤ i ≤ n ( x i ′ ) − mi n 1 ≤ j ≤ n ( x j ′ ) , ma x 1 ≤ i ≤ n ( y i ′ ) − mi n 1 ≤ j ≤ n ( y j ′ ))
Since finding the maximum and minimum of x + y x+y x + y and x − y x-y x − y respectively is O ( m ) O(m) O ( m ) , the problem can be solved.
Incidentally, when extended to higher-dimensional spaces, for R k R^k R k the complexity is ( O ( k 2 k m ) ) (O(k2^km)) ( O ( k 2 k m )) . Quite challenging, indeed.
Create an issue on GitHub about this article