Skip to content
Related Articles
Find the integer points (x, y) with Manhattan distance atleast N
• Last Updated : 25 May, 2021

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.
Examples:

```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)```

Approach:

• 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:

## C++

 `// C++ code to Find the integer points (x, y)``// with Manhattan distance atleast N` `#include ``using` `namespace` `std;` `// C++ function to find all possible point``vector > FindPoints(``int` `n)``{` `    ``vector > v;` `    ``// Find all 4 corners of the square``    ``// whose side length is n``    ``v.push_back({ 0, 0 });``    ``v.push_back({ 0, n });``    ``v.push_back({ n, 0 });``    ``v.push_back({ n, n });` `    ``// If n is even then the middle point``    ``// of the square will be an integer,``    ``// so we will take that point``    ``if` `(n % 2 == 0)``        ``v.push_back({ n / 2, n / 2 });` `    ``return` `v;``}` `// Driver Code``int` `main()``{` `    ``int` `N = 8;` `    ``vector > v``        ``= FindPoints(N);` `    ``// Printing all possible points``    ``for` `(``auto` `i : v) {``        ``cout << ``"("` `<< i.first << ``", "``             ``<< i.second << ``") "``;``    ``}``    ``return` `0;``}`

## Java

 `// Java code to Find the integer points (x, y)``// with Manhattan distance atleast N``import` `java.util.*;` `class` `GFG``{` `static` `class` `pair``{``    ``int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}` `// Java function to find all possible point``static` `Vector FindPoints(``int` `n)``{``    ``Vector v = ``new` `Vector();` `    ``// Find all 4 corners of the square``    ``// whose side length is n``    ``v.add(``new` `pair( ``0``, ``0` `));``    ``v.add(``new` `pair( ``0``, n ));``    ``v.add(``new` `pair( n, ``0` `));``    ``v.add(``new` `pair( n, n ));` `    ``// If n is even then the middle point``    ``// of the square will be an integer,``    ``// so we will take that point``    ``if` `(n % ``2` `== ``0``)``        ``v.add(``new` `pair( n / ``2``, n / ``2` `));` `    ``return` `v;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``8``;` `    ``Vector v = FindPoints(N);` `    ``// Printing all possible points``    ``for` `(pair i : v)``    ``{``        ``System.out.print(``"("` `+ i.first + ``", "` `+``                               ``i.second + ``") "``);``    ``}``}``}` `// This code is contributed by PrinciRaj1992`

## Python3

 `# Python3 code to Find the integer points (x, y)``# with Manhattan distance atleast N` `# function to find all possible point``def` `FindPoints(n) :` `    ``v ``=` `[];` `    ``# Find all 4 corners of the square``    ``# whose side length is n``    ``v.append([ ``0``, ``0` `]);``    ``v.append([ ``0``, n ]);``    ``v.append([ n, ``0` `]);``    ``v.append([ n, n ]);` `    ``# If n is even then the middle point``    ``# of the square will be an integer,``    ``# so we will take that point``    ``if` `(n ``%` `2` `=``=` `0``) :``        ``v.append([ n ``/``/` `2``, n ``/``/` `2` `]);` `    ``return` `v;` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``N ``=` `8``;` `    ``v ``=` `FindPoints(N);` `    ``# Printing all possible points``    ``for` `element ``in` `v :``        ``print``(``"("``, element[``0``],``              ``","``, element[``1``], ``")"``, end ``=` `" "``);` `# This code is contributed by AnkitRai01`

## C#

 `// C# code to Find the integer points (x, y)``// with Manhattan distance atleast N``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `class` `pair``{``    ``public` `int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}` `// Function to find all possible point``static` `List FindPoints(``int` `n)``{``    ``List v = ``new` `List();` `    ``// Find all 4 corners of the square``    ``// whose side length is n``    ``v.Add(``new` `pair( 0, 0 ));``    ``v.Add(``new` `pair( 0, n ));``    ``v.Add(``new` `pair( n, 0 ));``    ``v.Add(``new` `pair( n, n ));` `    ``// If n is even then the middle point``    ``// of the square will be an integer,``    ``// so we will take that point``    ``if` `(n % 2 == 0)``        ``v.Add(``new` `pair( n / 2, n / 2 ));` `    ``return` `v;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 8;` `    ``List v = FindPoints(N);` `    ``// Printing all possible points``    ``foreach` `(pair i ``in` `v)``    ``{``        ``Console.Write(``"("` `+ i.first + ``", "` `+``                            ``i.second + ``") "``);``    ``}``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``
Output:
`(0, 0) (0, 8) (8, 0) (8, 8) (4, 4)`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up