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 = 5Output:(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 4Input:d1 = 5, d2 = 10, d3 = 12Output:-1

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

## C++

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the original coordinated` `void` `solve(` `int` `d1, ` `int` `d2, ` `int` `d3)` `{` ` ` `// Maximum of the given distances` ` ` `int` `maxx = max(d1, max(d2, d3));` ` ` `// Sum of the given distances` ` ` `int` `sum = (d1 + d2 + d3);` ` ` `// Conditions when the` ` ` `// solution doesn't exist` ` ` `if` `(2 * maxx > sum or sum % 2 == 1) {` ` ` `cout << ` `"-1"` `;` ` ` `return` `;` ` ` `}` ` ` `// First coordinate` ` ` `int` `x1 = 0, y1 = 0;` ` ` `// Second coordinate` ` ` `int` `x2 = d1, y2 = 0;` ` ` `// Third coordinate` ` ` `int` `x3 = (d1 + d2 - d3) / 2;` ` ` `int` `y3 = (d2 + d3 - d1) / 2;` ` ` `cout << ` `"("` `<< x1 << ` `", "` `<< y1 << ` `"), ("` ` ` `<< x2 << ` `", "` `<< y2 << ` `") and ("` ` ` `<< x3 << ` `", "` `<< y3 << ` `")"` `;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `d1 = 3, d2 = 4, d3 = 5;` ` ` `solve(d1, d2, d3);` ` ` `return` `0;` `}` |

## Java

`// Java implementation of the approach` `import` `java .io.*;` `class` `GFG` `{` ` ` `// Function to find the original coordinated` `static` `void` `solve(` `int` `d1, ` `int` `d2, ` `int` `d3)` `{` ` ` `// Maximum of the given distances` ` ` `int` `maxx = Math.max(d1, Math.max(d2, d3));` ` ` `// Sum of the given distances` ` ` `int` `sum = (d1 + d2 + d3);` ` ` `// Conditions when the` ` ` `// solution doesn't exist` ` ` `if` `(` `2` `* maxx > sum || sum % ` `2` `== ` `1` `)` ` ` `{` ` ` `System.out.print(` `"-1"` `);` ` ` `return` `;` ` ` `}` ` ` `// First coordinate` ` ` `int` `x1 = ` `0` `, y1 = ` `0` `;` ` ` `// Second coordinate` ` ` `int` `x2 = d1, y2 = ` `0` `;` ` ` `// Third coordinate` ` ` `int` `x3 = (d1 + d2 - d3) / ` `2` `;` ` ` `int` `y3 = (d2 + d3 - d1) / ` `2` `;` ` ` `System.out.print(` `"("` `+x1+` `", "` `+y1+` `"), ("` `+x2+` `", "` `+y2+` `") and ("` `+x3+` `", "` `+y3+` `")"` `);` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `d1 = ` `3` `, d2 = ` `4` `, d3 = ` `5` `;` ` ` `solve(d1, d2, d3);` `}` `}` `// This code is contributed by anuj_67..` |

```
# Python3 implementation of the approach
# Function to find the original coordinated
def solve(d1, d2, d3) :
# Maximum of the given distances
maxx = max(d1, max(d2, d3))
# Sum of the given distances
sum = (d1 + d2 + d3)
# Conditions when the
# solution doesn't exist
if (2 * maxx > sum or sum % 2 == 1) :
print("-1")
return
# First coordinate
x1 = 0
y1 = 0
# Second coordinate
x2 = d1
y2 = 0
# Third coordinate
x3 = (d1 + d2 - d3) // 2
y3 = (d2 + d3 - d1) // 2
print("(" , x1 , "," , y1 , "), ("
, x2 , "," , y2 , ") and ("
, x3 , "," , y3 , ")")
# Driver code
d1 = 3
d2 = 4
d3 = 5
solve(d1, d2, d3)
# This code is contributed by ihritik
```

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to find the original coordinated` `static` `void` `solve(` `int` `d1, ` `int` `d2, ` `int` `d3)` `{` ` ` `// Maximum of the given distances` ` ` `int` `maxx = Math.Max(d1, Math.Max(d2, d3));` ` ` `// Sum of the given distances` ` ` `int` `sum = (d1 + d2 + d3);` ` ` `// Conditions when the` ` ` `// solution doesn't exist` ` ` `if` `(2 * maxx > sum || sum % 2 == 1)` ` ` `{` ` ` `Console.WriteLine(` `"-1"` `);` ` ` `return` `;` ` ` `}` ` ` `// First coordinate` ` ` `int` `x1 = 0, y1 = 0;` ` ` `// Second coordinate` ` ` `int` `x2 = d1, y2 = 0;` ` ` `// Third coordinate` ` ` `int` `x3 = (d1 + d2 - d3) / 2;` ` ` `int` `y3 = (d2 + d3 - d1) / 2;` ` ` `Console.WriteLine(` `"("` `+x1+` `", "` `+y1+` `"), ("` `+x2+` `", "` `+y2+` `") and ("` `+x3+` `", "` `+y3+` `")"` `);` `}` `// Driver code` `static` `void` `Main()` `{` ` ` `int` `d1 = 3, d2 = 4, d3 = 5;` ` ` `solve(d1, d2, d3);` `}` `}` `// This code is contributed by mits` |

## Javascript

`<script>` `// Javascript implementation of the approach` `// Function to find the original coordinated` `function` `solve(d1, d2, d3)` `{` ` ` `// Maximum of the given distances` ` ` `let maxx = Math.max(d1, Math.max(d2, d3));` ` ` `// Sum of the given distances` ` ` `let sum = (d1 + d2 + d3);` ` ` `// Conditions when the` ` ` `// solution doesn't exist` ` ` `if` `(2 * maxx > sum || sum % 2 == 1) {` ` ` `document.write(` `"-1"` `);` ` ` `return` `;` ` ` `}` ` ` `// First coordinate` ` ` `let x1 = 0, y1 = 0;` ` ` `// Second coordinate` ` ` `let x2 = d1, y2 = 0;` ` ` `// Third coordinate` ` ` `let x3 = parseInt((d1 + d2 - d3) / 2);` ` ` `let y3 = parseInt((d2 + d3 - d1) / 2);` ` ` `document.write(` `"("` `+ x1 + ` `", "` `+ y1 + ` `"), ("` ` ` `+ x2 + ` `", "` `+ y2 + ` `") and ("` ` ` `+ x3 + ` `", "` `+ y3 + ` `")"` `);` `}` `// Driver code` ` ` `let d1 = 3, d2 = 4, d3 = 5;` ` ` `solve(d1, d2, d3);` `</script>` |

**Output:**

(0, 0), (3, 0) and (1, 3)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**