Minkowski Distance

MediumMachine LearningMathSortingTree

Description

Given two equal-length vectors and an order p, return the Minkowski distance: the p-th root of the sum of absolute coordinate differences each raised to the power p. Round the result to 4 decimal places.

Examples

Input:[0,0], [3,4], 2
Output:5
Explanation:

Minkowski generalizes the family of distances, becoming Manhattan at order one and Euclidean at order two.

Input:[1,2,3], [4,6,8], 1
Output:12
Explanation:

Minkowski generalizes the family of distances, becoming Manhattan at order one and Euclidean at order two.

Input:[0,0], [1,1], 3
Output:1.2599
Explanation:

Minkowski generalizes the family of distances, becoming Manhattan at order one and Euclidean at order two.

Constraints

  • 1 ≤ length ≤ 10⁴
  • 1 ≤ p ≤ 5

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.