VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minkowski-distance/

⇱ Program to find Minkowski Distance - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find Minkowski Distance

Last Updated : 23 Jul, 2025

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.340

Input: 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:

  • Create a variable let say X.
  • Run a loop and follow below mentioned steps under the scope of loop:
    • X += Power ((Ai - Bi), P)
  • Calculate Z as (1/P)
  • Return Power(X, Z)

Below is the implementation of the above approach:


Output
8

Time Complexity: O(n * log2(p))
Auxiliary Space: O(1)

Related Articles:

Comment