Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 −
#includeusing namespace std; #define N 100 void solve(int x, int y) { if(x % 2 == 0 && y % 2 == 0) cout Input
13, 7Output
6 4
Advertisements
