FastPrepMaximum L1 Distance Between Equal-Length Subarrays
Problems/Maximum L1 Distance Between Equal-Length Subarrays

Maximum L1 Distance Between Equal-Length Subarrays

The D.E. Shaw Group logoThe D.E. Shaw Group● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source specifies the two-array equal-length subarray objective and that length 1 is allowed, but omits finite input bounds and the numeric return width. The judged core task matches the visible source at about 98%.

Given two integer arrays a and b, choose one nonempty contiguous subarray from each array. The two chosen subarrays must have the same length, but they may start at different indices.

If the chosen subarrays start at indices i and j and have length length, their L1 distance is

|a[i] - b[j]| + |a[i + 1] - b[j + 1]| + ... + |a[i + length - 1] - b[j + length - 1]|

Return the maximum possible L1 distance. A subarray of length 1 is valid.

Function

maxL1Distance(a: int[], b: int[]) → long

Examples

Example 1

a = [1,3,2]b = [4,2]return = 4

Choose [1,3] from a and [4,2] from b. Their distance is |1 - 4| + |3 - 2| = 3 + 1 = 4.

Example 2

a = [100,0]b = [100,-100]return = 200

Choose the length-1 subarrays [100] from the start of a and [-100] from the end of b. Their distance is 200.

Example 3

a = [-3,4]b = [1,-2,5]return = 10

Align [-3,4] with [1,-2]. The distance is |-3 - 1| + |4 - (-2)| = 4 + 6 = 10, which is maximal.

Constraints

  • 1 <= a.length, b.length <= 2000
  • -10^9 <= a[i], b[i] <= 10^9
  • The answer fits in a signed 64-bit integer.

More The D.E. Shaw Group problems

  • Koko Eating BananasPHONE SCREEN · Seen Nov 2026
  • Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
  • Count Lucky TicketsOA · Seen Nov 2026
All The D.E. Shaw Group interview questionsSee The D.E. Shaw Group hiring insights
Saved
class Solution {
    public long maxL1Distance(int[] a, int[] b) {
        // Write your code here.
    }
}
Checking account…