Given the Manhattan distances of three coordinates on a 2-D plane, the task is to find the original coordinates. Print any solution if multiple solutions are possible else print -1.
Input: d1 = 3, d2 = 4, d3 = 5
Output: (0, 0), (3, 0) and (1, 3)
Manhattan distance between (0, 0) to (3, 0) is 3,
(3, 0) to (1, 3) is 5 and (0, 0) to (1, 3) is 4
Input: d1 = 5, d2 = 10, d3 = 12
Approach: Let’s analyze when no solution exists. First the triangle inequality must hold true i.e. the largest distance should not exceed the sum of other two. Second, sum of all Manhattan distances should be even.
Here’s why, if we have three points and their x-coordinates are x1, x2 and x3 such that x1 < x2 < x3. They will contribute to the sum (x2 – x1) + (x3 – x1) + (x3 – x2) = 2 * (x3 – x1). Same logic applied for y-coordinates.
In all the other cases, we have a solution. Let d1, d2 and d3 be the given Manhattan distances. Fix two points as (0, 0) and (d1, 0). Now since two points are fixed, we can easily find the third point as x3 = (d1 + d2 – d3) / 2 and y3 = (d2 – x3).
Below is the implementation of the above approach:
(0, 0), (3, 0) and (1, 3)
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.
- Find a point such that sum of the Manhattan distances is minimized
- Sum of Manhattan distances between all pairs of points
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Maximum Manhattan distance between a distinct pair from N coordinates
- Find the coordinates of a triangle whose Area = (S / 2)
- Find the integer points (x, y) with Manhattan distance atleast N
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Pairs with same Manhattan and Euclidean distance
- Count paths with distance equal to Manhattan distance
- Find the distance covered to collect items at equal distances
- C/C++ program to add N distances given in inch-feet system using Structures
- Minimum Sum of Euclidean Distances to all given Points
- Sum of all distances between occurrences of same characters in a given string
- Program to find the Type of Triangle from the given Coordinates
- Find coordinates of the triangle given midpoint of each side
- Find minimum area of rectangle with given set of coordinates
- Maximum integral co-ordinates with non-integer distances
- Find the original matrix when largest element in a row and a column are given
- Find the deleted value from the array when average of original elements is given
- Find coordinates of a prime number in a Prime Spiral
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.