Given a number N, the task is to find the integer points (x, y) such that 0 <= x, y <= N and Manhattan distance between any two points will be atleast N.
Input: N = 3 Output: (0, 0) (0, 3) (3, 0) (3, 3) Input: N = 4 Output: (0, 0) (0, 4) (4, 0) (4, 4) (2, 2)
- Manhattan Distance between two points (x1, y1) and (x2, y2) is:
|x1 – x2| + |y1 – y2|
- Here for all pair of points this distance will be atleast N.
- As 0 <= x <= N and 0 <= y <= N so we can imagine a square of side length N whose bottom left corner is (0, 0) and top right corner is (N, N).
- So if we place 4 points in this corner then Manhattan distance will be atleast N.
- Now as we have to maximize the number of the point we have to check is there any available point inside the square.
- If N is even then middle point of the square which is (N/2, N/2) is integer point, otherwise, it will be float value as N/2 is not a integer when N is odd.
- So the only available position is the middle point and we can put a point there only if N is even.
- So number of points will be 4 if N is odd and if N is even then the number of points will be 5.
Below is the implementation of the above approach:
(0, 0) (0, 8) (8, 0) (8, 8) (4, 4)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the number of points that have atleast 1 point above, below, left or right of it
- Count paths with distance equal to Manhattan distance
- Sum of Manhattan distances between all pairs of points
- Pairs with same Manhattan and Euclidean distance
- Find the maximum possible distance from origin using given points
- Find points at a given distance on a line of given slope
- Haversine formula to find distance between two points on a sphere
- Find integral points with minimum distance from given set of integers using BFS
- Ways to choose three points with distance between the most distant points <= L
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Program to calculate distance between two points
- Program for distance between two points on earth
- Hammered distance between N points in a 2-D plane
- Program to calculate distance between two points in 3 D
- Find the original coordinates whose Manhattan distances are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.