C++ code to find a point that satisfies the constraints

Suppose, we are given two points a = (x1, y1) and b = (x2, y2). The Manhattan distance between two points is dist(a, b) = |x1 - x2| + |y1 - y2|. If the point a's coordinate is (0, 0) and the point b's coordinate is (x, y), we have to find a point c such that dist(a, c) = dist(a, b)/ 2 and dist(b, c) = dist(a, b)/2. If a point like that is not available then, we print -1, -1.

So, if the input is like x = 13, y = 7, then the output will be 6, 4.

Steps

To solve this, we will follow these steps −

if x mod 2 is same as 0 and y mod 2 is same as 0, then:
   print( x / 2, y / 2)
otherwise when (x + y) mod 2 is same as 1, then:
   print(- 1, - 1)
Otherwise,
   print(x / 2, (y + 1) / 2)

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
#define N 100
void solve(int x, int y) {
   if(x % 2 == 0 && y % 2 == 0)
      cout

Input

13, 7

Output

6 4
Updated on: 2022-03-29T08:54:56+05:30

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements