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++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , int > > FindPoints( int n)
{
vector<pair< int , int > > v;
v.push_back({ 0, 0 });
v.push_back({ 0, n });
v.push_back({ n, 0 });
v.push_back({ n, n });
if (n % 2 == 0)
v.push_back({ n / 2, n / 2 });
return v;
}
int main()
{
int N = 8;
vector<pair< int , int > > v
= FindPoints(N);
for ( auto i : v) {
cout << "(" << i.first << ", "
<< i.second << ") " ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static Vector<pair> FindPoints( int n)
{
Vector<pair> v = new Vector<pair>();
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 % 2 == 0 )
v.add( new pair( n / 2 , n / 2 ));
return v;
}
public static void main(String[] args)
{
int N = 8 ;
Vector<pair > v = FindPoints(N);
for (pair i : v)
{
System.out.print( "(" + i.first + ", " +
i.second + ") " );
}
}
}
|
Python3
def FindPoints(n) :
v = [];
v.append([ 0 , 0 ]);
v.append([ 0 , n ]);
v.append([ n, 0 ]);
v.append([ n, n ]);
if (n % 2 = = 0 ) :
v.append([ n / / 2 , n / / 2 ]);
return v;
if __name__ = = "__main__" :
N = 8 ;
v = FindPoints(N);
for element in v :
print ( "(" , element[ 0 ],
"," , element[ 1 ], ")" , end = " " );
|
C#
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;
}
}
static List<pair> FindPoints( int n)
{
List<pair> v = new List<pair>();
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 % 2 == 0)
v.Add( new pair( n / 2, n / 2 ));
return v;
}
public static void Main(String[] args)
{
int N = 8;
List<pair > v = FindPoints(N);
foreach (pair i in v)
{
Console.Write( "(" + i.first + ", " +
i.second + ") " );
}
}
}
|
Javascript
<script>
function FindPoints(n)
{
var v = [];
v.push([ 0, 0 ]);
v.push([ 0, n ]);
v.push([ n, 0 ]);
v.push([ n, n ]);
if (n % 2 == 0)
v.push([ n / 2, n / 2 ]);
return v;
}
var N = 8;
var v = FindPoints(N);
v.forEach(i => {
document.write( "(" + i[0] + ", "
+ i[1] + ") " );
});
</script>
|
Output: (0, 0) (0, 8) (8, 0) (8, 8) (4, 4)
Time Complexity: O(1)
Auxiliary Space: O(1)