Maximum Distance in Arrays

Updated on 09 June, 2025
Maximum Distance in Arrays header image

Problem Statement

Given several arrays where each individual array is sorted in ascending order, the task is to determine the largest possible absolute difference between any two elements, each taken from a different array. The absolute difference between two integers a and b is defined as |a - b|. The goal is to compute this maximum distance to understand how spread apart the elements from two distinct arrays can be.

Examples

Example 1

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.

Example 2

Input:

arrays = [[1],[1]]

Output:

0

Constraints

  • m == arrays.length
  • 2 <= m <= 105
  • 1 <= arrays[i].length <= 500
  • -104 <= arrays[i][j] <= 104
  • arrays[i] is sorted in ascending order.
  • There will be at most 105 integers in all the arrays.

Approach and Intuition

When attempting to solve the problem of finding the maximum distance by selecting one number from two different arrays, consider the following approach:

  1. Recognize that the maximum distance can only be achieved by considering the smallest or largest values in each array because the arrays are sorted. The minimum or maximum values will inherently provide the greatest difference.

  2. To maximize the absolute difference |a - b|, one would typically look to subtract the smallest number in one array from the largest number in another array.

    • Given the data within two arrays [1, 3, 5] and [2, 6, 8], the potentially largest difference would be between the smallest number of the first array (1) and the largest number of the second array (8) or vice versa.
  3. Practical step-by-step plan:

    • Initialize two variables, globalMin and globalMax, to track the maximum of min values from all arrays and the minimum of max values across all arrays, respectively.
    • Loop through each array:
      1. For each array, identify its local minimum and maximum.
      2. Update globalMax and globalMin by comparing current array's values with global values.
    • Finally, the desired maximum distance will be the difference between globalMax and globalMin.

Considering the constraints:

  • Due to the problem's requirements, the approach above is efficient because it processes each array individually and makes use of the property that each individual array is already sorted.
  • The operations per array are constant and only involve retrieving the first and last elements due to the arrays' sorted nature.
  • This results in a time complexity proportional to the number of arrays, making it scalable up to the upper limit of the provided constraints.

Solutions

  • Java
java
class Solution {
    public int findMaxDiff(List<List<Integer>> listArrays) {
        int maximumDiff = 0;
        int firstSize = listArrays.get(0).size();
        int smallestValue = listArrays.get(0).get(0);
        int largestValue = listArrays.get(0).get(firstSize - 1);
        for (int index = 1; index < listArrays.size(); index++) {
            firstSize = listArrays.get(index).size();
            maximumDiff = Math.max(maximumDiff, Math.max(Math.abs(listArrays.get(index).get(firstSize - 1) - smallestValue), 
                                                         Math.abs(largestValue - listArrays.get(index).get(0))));
            smallestValue = Math.min(smallestValue, listArrays.get(index).get(0));
            largestValue = Math.max(largestValue, listArrays.get(index).get(firstSize - 1));
        }
        return maximumDiff;
    }
}

This Java solution tackles the problem of finding the maximal distance (difference) between elements in an array of arrays. It particularly focuses on determining the maximum difference between the minimum element of any array and the maximum element of any other array within a provided list of lists.

Follow these implementation details from the provided Java method:

  1. Initialize maximumDiff to zero to keep track of the maximum difference found.
  2. Extract the size firstSize of the first array and assign values to smallestValue and largestValue from the first array to initially represent the globally smallest and largest values in the list of lists.
  3. Iterate through the list starting from the second array. During each iteration:
    • Update maximumDiff by comparing it with the absolute difference between the last element of the current array and smallestValue, as well as with the absolute difference between largestValue and the first element of the current array.
    • Update smallestValue to the smaller of itself and the first element of the current array.
    • Update largestValue to the larger of itself and the last element of the current array.
  4. Return the maximumDiff which represents the maximum distance found across all given arrays.

This approach ensures efficient scanning of the list to find the maximal distance, leveraging only a single scan (O(n), where n is the number of arrays in the list). The use of auxiliary variables helps in keeping track of the smallest and largest values dynamically as the list is processed.

Comments

No comments yet.