Mid/SeniorArrayGreedy

Maximum Distance in Arrays

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

You can pick two integers from two different arrays, choosing one integer from each array, and calculate the distance between them. The distance between two integers a and b is defined as their absolute difference |a - b|.

Return the maximum distance.

Example 1
Inputarrays = [[1,2,3],[4,5],[1,2,3]]
Output4
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.
Example 2
Inputarrays = [[1],[1]]
Output0
Both arrays contain only 1, so the maximum distance is 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.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate