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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Find integral points with minimum distance from given set of integers using BFS
- Haversine formula to find distance between two points on a sphere
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- 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
- Find the original coordinates whose Manhattan distances are given
- Find a point such that sum of the Manhattan distances is minimized
- Program to calculate distance between two points in 3 D
- Program for distance between two points on earth
- Program to calculate distance between two points
- Hammered distance between N points in a 2-D plane
- Check whether it is possible to join two points given on circle such that distance between them is k
- Sort an Array of Points by their distance from a reference Point
- Distance between end points of Hour and minute hand at given time
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.