![]() |
VOOZH | about |
Given two arrays A[] and B[] as position vector of two points in n-dimensional space along with an integer p, the task is to calculate Minkowski Distance between these two points.
The Minkowski distance is a generalization of other distance measures, such as Euclidean and Manhattan distances, and is defined as:
where A and B are vectors representing points in the multidimensional space, n is the number of dimensions, and p is a positive constant known as the order parameter.
Examples:
Input: A[] = {1,2,3,4}, B[] = {5,6,7,8}, P = 3
Output: 6.340Input: A = {1,2,3,4}, B[] = {5,6,7,8} P = 2
Output: 8
Approach:
Traverse using loop and calculate X as {(A1 - B1)P + (A2 - B2)P . . . (AN - BN)P}. Then calculate Minkowski Distance as X(1/P)
Step-by-step approach:
Below is the implementation of the above approach:
8
Time Complexity: O(n * log2(p))
Auxiliary Space: O(1)
Related Articles: