FastPrepMinimum Difference Across Cyclic Rotations

Minimum Difference Across Cyclic Rotations

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

You are given equal-length integer arrays first and second. For every cyclic left rotation of first, sum the absolute differences of aligned elements.

Return the minimum sum across all rotations.

Function

minimumRotationDifference(first: int[], second: int[]) → long

Examples

Example 1

first = [1,2,3]second = [2,3,1]return = 0

One left rotation makes the arrays equal.

Example 2

first = [1,4]second = [2,8]return = 5

The unrotated cost is 1 + 4 = 5, which is minimal.

Constraints

  • 1 <= first.length == second.length <= 2000
  • -1000000000 <= value <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public long minimumRotationDifference(int[] first, int[] second) {
    // Write your code here.
}
first[1,2,3]
second[2,3,1]
expected0
Checking account…