> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Distance in Arrays Python Solution

> Tested Python solution for LeetCode 624 with 11 pytest cases. Generate a practice environment with lcpy.

LeetCode 624, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/maximum-distance-in-arrays/description/).

Generate this problem as a practice environment: tested reference solution, 11 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 624   # by problem number
lcpy gen -s maximum_distance_in_arrays   # by problem name
```

## Problem

You are given `m` `arrays`, where each array is sorted in **ascending order**.

You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers `a` and `b` to be their absolute difference `|a - b|`.

Return *the maximum distance*.

### Examples

```
Input: arrays = [[1,2,3],[4,5],[1,2,3]]
Output: 4
Explanation: One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
```

```
Input: arrays = [[1],[1]]
Output: 0
```

### Constraints

* m == arrays.length
* 2 \<= m \<= 10^5
* 1 \<= arrays\[i].length \<= 500
* -10^4 \<= arrays\[i]\[j] \<= 10^4
* arrays\[i] is sorted in ascending order.
* There will be at most 10^5 integers in all the arrays.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_distance_in_arrays/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_distance_in_arrays/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m) where m = len(arrays)
    # Space: O(1)
    def max_distance(self, arrays: list[list[int]]) -> int:
        result = 0
        cur_min, cur_max = arrays[0][0], arrays[0][-1]
        for arr in arrays[1:]:
            # Pair the current array against the best extremes seen so far;
            # avoids using two extremes from the same array.
            result = max(result, arr[-1] - cur_min, cur_max - arr[0])
            cur_min = min(cur_min, arr[0])
            cur_max = max(cur_max, arr[-1])
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(m) where m = len(arrays) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.