Given an integer N, Find 4 points in a 2D plane having integral coordinates, such that the Manhattan distance between any pair of points is equal to N.
Examples:
Input: N = 6
Output: { {0, -3}, {3, 0}, {-3, 0}, {0, 3} }
Explanation: It can be easily calculated that Manhattan distance between all possible pairs is 6.
Input: N = 11
Output: -1
Explanation: It is not possible to locate 4 points such that Manhattan distance between all pairs is 11
Approach: The idea to solve the problem is based on the following observation:
Square having all 4 vertices on all 4 co-ordinate axes (1 vertex on each axis) equidistant from origin have the same distance between any pair of vertices which is the double of the distance between any vertex and origin.
If N is odd, it can not be divided into 2 equal integral parts. So, a square can not be formed such that opposite vertices are at equal distance from the origin (i.e. N/2), with distance between them being N, for such a case 4 points satisfying given condition cannot be found.
Follow the steps mentioned below to solve the problem:
- Check if N is odd or even.
- If N is odd then such 4 points cannot be found.
- Otherwise set the four points as {N/2, 0}, {-N/2, 0}, {0, N/2}, {0, -N/2}
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 > > points;
if (N % 2 == 1)
return points;
int point = N / 2;
points.push_back({ 0, point });
points.push_back({ 0, -point });
points.push_back({ point, 0 });
points.push_back({ -point, 0 });
return points;
}
void print( int N)
{
vector<pair< int , int > > ans
= findPoints(N);
if (ans.size() == 0)
cout << -1;
for ( int i = 0; i < ans.size(); i++) {
cout << ans[i].first << ", "
<< ans[i].second << "\n" ;
}
}
int main()
{
int N = 6;
print(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int [][] findPoints( int N)
{
int [][]points= new int [ 4 ][ 2 ];
if (N % 2 == 1 )
return points;
int point = N / 2 ;
points[ 0 ][ 0 ] = 0 ;
points[ 0 ][ 1 ] = point;
points[ 1 ][ 0 ] = 0 ;
points[ 1 ][ 1 ] = -point;
points[ 2 ][ 0 ] = point;
points[ 2 ][ 1 ] = 0 ;
points[ 3 ][ 0 ] = -point;
points[ 3 ][ 1 ] = 0 ;
return points;
}
static void print( int N)
{
int [][] ans = findPoints(N);
if (ans.length == 0 )
System.out.print(- 1 );
for ( int i = 0 ; i < ans.length; i++) {
System.out.println(ans[i][ 0 ] + ", " + ans[i][ 1 ]);
}
}
public static void main (String[] args) {
int N = 6 ;
print(N);
}
}
|
Python3
def findPoints(N):
points = []
if (N % 2 = = 1 ):
return points
point = (N / / 2 )
points.append([ 0 ,point ])
points.append([ 0 , - 1 * point ])
points.append([point, 0 ])
points.append([ - 1 * point, 0 ])
return points
def Print (N):
ans = findPoints(N)
if ( len (ans) = = 0 ):
print ( - 1 )
for i in range ( len (ans)):
print ( str (ans[i][ 0 ]) + ", " + str (ans[i][ 1 ]))
N = 6
Print (N)
|
C#
using System;
class GFG {
static int [, ] findPoints( int N)
{
int [, ] points = new int [4, 2];
if (N % 2 == 1)
return points;
int point = N / 2;
points[0, 0] = 0;
points[0, 1] = point;
points[1, 0] = 0;
points[1, 1] = -point;
points[2, 0] = point;
points[2, 1] = 0;
points[3, 0] = -point;
points[3, 1] = 0;
return points;
}
static void print( int N)
{
int [, ] ans = findPoints(N);
if (ans.GetLength(0) == 0)
Console.Write(-1);
for ( int i = 0; i < ans.GetLength(0); i++) {
Console.WriteLine(ans[i, 0] + ", " + ans[i, 1]);
}
}
public static void Main()
{
int N = 6;
print(N);
}
}
|
Javascript
<script>
function findPoints(N) {
let points = [];
if (N % 2 == 1)
return points;
let point = Math.floor(N / 2);
points.push({ first: 0, second: point });
points.push({ first: 0, second: -1 * point });
points.push({ first: point, second: 0 });
points.push({ first: -1 * point, second: 0 });
return points;
}
function print(N) {
let ans = findPoints(N);
if (ans.length == 0)
document.write(-1);
for (let i = 0; i < ans.length; i++) {
document.write(ans[i].first + ", "
+ ans[i].second + '<br>' );
}
}
let N = 6;
print(N);
</script>
|
Output
0, 3
0, -3
3, 0
-3, 0
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Apr, 2022
Like Article
Save Article