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