Given N points in K dimensional space where,
and
. The task is to determine the point such that the sum of Manhattan distances from this point to the N points is minimized.
Manhattan distance is the distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 – x2| + |y1 – y2|.
Examples:
Input: N = 3, K = 3, Points = {1, 1, 1}, {2, 2, 2}, {3, 3, 3}
Output: 2 2 2
Input: N = 4, K = 4, Points = {1, 6, 9, 6}, {5, 2, 5, 7}, {2, 0, 1, 5}, {4, 6, 3, 9}
Output: 2 2 3 6
Approach: To minimize the Manhattan distance all we have to do is to just sort the points in all K dimensions and output the middle elements of each of the K dimensions.
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to print the required points which // minimizes the sum of Manhattan distances void minDistance(int n, int k, vector<vector<int> >& point) { // Sorting points in all k dimension for (int i = 0; i < k; ++i) sort(point[i].begin(), point[i].end()); // Output the required k points for (int i = 0; i < k; ++i) cout << point[i][(ceil((double)n / 2) - 1)] << " "; } // Driver code int main() { int n = 4, k = 4; vector<vector<int> > point = { { 1, 5, 2, 4 }, { 6, 2, 0, 6 }, { 9, 5, 1, 3 }, { 6, 7, 5, 9 } }; // function call to print required points minDistance(n, k, point); return 0; } |
Java
// Java implementation of above approach import java.util.Arrays; class GFG { // Function to print the required // points which minimizes the sum // of Manhattan distances static void minDistance(int n, int k, int point[][]) { // Sorting points in all k dimension for (int i = 0; i < k; i++) Arrays.sort(point[i]); // Output the required k points for (int i = 0; i < k; i++) System.out.print(point[i][(int) Math.ceil((double)(n / 2) - 1)] + " "); } // Driver code public static void main(String[] args) { int n = 4; int k = 4; int point[][] = { { 1, 5, 2, 4 }, { 6, 2, 0, 6 }, { 9, 5, 1, 3 }, { 6, 7, 5, 9 } }; // function call to print required points minDistance(n, k, point); } } // This code is contributed by Bilal |
Python
# Python implementation of above approach # Function to print the required points which # minimizes the sum of Manhattan distances def minDistance(n, k, point): # Sorting points in all dimension for i in range(k): point[i].sort() # Output the required k points for i in range(k): print(point[i][((n + 1) // 2) - 1], end =" ") # Driver code n = 4k = 4point = [[1, 5, 2, 4], [6, 2, 0, 6], [9, 5, 1, 3], [6, 7, 5, 9]] # function call to print required points minDistance(n, k, point) |
C#
// C# implementation of above approach using System; class GFG { // Function to print the required // points which minimizes the sum // of Manhattan distances static void minDistance(int n, int k, int[][] point) { // Sorting points in all k dimension for (int i = 0; i < k; i++) Array.Sort(point[i]); // Output the required k points for (int i = 0; i < k; i++) System.Console.Write(point[i][(int) Math.Ceiling((double)(n / 2) - 1)] + " "); } // Driver code public static void Main() { int n = 4; int k = 4; int[][] point = new int[][]{ new int[]{ 1, 5, 2, 4 }, new int[]{ 6, 2, 0, 6 }, new int[]{ 9, 5, 1, 3 }, new int[]{ 6, 7, 5, 9 } }; // function call to print required points minDistance(n, k, point); } } // This code is contributed by mits |
PHP
<?php // PHP implementation of above approach // Function to print the required // points which minimizes the sum // of Manhattan distances function minDistance($n, $k, &$point) { // Sorting points in all // k dimension for ($i = 0; $i < $k; ++$i) sort($point[$i]); // Output the required k points for ($i = 0; $i < $k; ++$i) echo $point[$i][(ceil( (double)$n / 2) - 1)] . " "; } // Driver code $n = 4; $k = 4; $point = array(array( 1, 5, 2, 4 ), array( 6, 2, 0, 6 ), array( 9, 5, 1, 3 ), array( 6, 7, 5, 9 )); // function call to print // required points minDistance($n, $k, $point); // This code is contributed // by ChitraNayal ?> |
2 2 3 6
Time Complexity: O(k*nlog(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of Manhattan distances between all pairs of points
- Find the original coordinates whose Manhattan distances are given
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Pair formation such that maximum pair sum is minimized
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Find the integer points (x, y) with Manhattan distance atleast N
- Choose k array elements such that difference of maximum and minimum is minimized
- Choose X such that (A xor X) + (B xor X) is minimized
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Pairs with same Manhattan and Euclidean distance
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Minimum Sum of Euclidean Distances to all given Points
- Sum of all distances between occurrences of same characters in a given string
- Find the distance covered to collect items at equal distances
- Partition the array into two odd length groups with minimized absolute difference between their median
- Select K elements from an array whose maximum value is minimized
- C/C++ program to add N distances given in inch-feet system using Structures
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

