Find 4 points with equal Manhattan distance between any pair
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++
// C++ code for above approach #include <bits/stdc++.h> using namespace std; // Function to find 4 points such that // manhattan distance between // any two of them is equal vector<pair< int , int > > findPoints( int N) { // Initializing vector of pairs to // store the 4 pairs vector<pair< int , int > > points; // If N is odd, it is impossible // to find 4 such points if (N % 2 == 1) return points; // Initializing a variable // with value equal to N/2 int point = N / 2; // Pushing all the 4 pairs into // vector "points" such that distance // between any two is equal points.push_back({ 0, point }); points.push_back({ 0, -point }); points.push_back({ point, 0 }); points.push_back({ -point, 0 }); // Returning "points" vector return points; } // Function to print 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" ; } } // Driver Code int main() { int N = 6; // Calling the print function print(N); return 0; } |
Java
// Java code for above approach import java.io.*; class GFG { // Function to find 4 points such that // manhattan distance between // any two of them is equal static int [][] findPoints( int N) { // Initializing vector of pairs to // store the 4 pairs int [][]points= new int [ 4 ][ 2 ]; // If N is odd, it is impossible // to find 4 such points if (N % 2 == 1 ) return points; // Initializing a variable // with value equal to N/2 int point = N / 2 ; // Pushing all the 4 pairs into // vector "points" such that distance // between any two is equal 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 ; // Returning "points" vector return points; } // Function to print 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 ]); } } // Driver Code public static void main (String[] args) { int N = 6 ; // Calling the print function print(N); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python code for the above approach # Function to find 4 points such that # manhattan distance between # any two of them is equal def findPoints(N): # Initializing vector of pairs to # store the 4 pairs points = [] # If N is odd, it is impossible # to find 4 such points if (N % 2 = = 1 ): return points # Initializing a variable # with value equal to N/2 point = (N / / 2 ) # Pushing all the 4 pairs into # vector "points" such that distance # between any two is equal points.append([ 0 ,point ]) points.append([ 0 , - 1 * point ]) points.append([point, 0 ]) points.append([ - 1 * point, 0 ]) # Returning "points" vector return points # Function to print 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 ])) # Driver Code N = 6 # Calling the print function Print (N) # This code is contributed by shinjanpatra |
C#
// C# code for above approach using System; class GFG { // Function to find 4 points such that // manhattan distance between // any two of them is equal static int [, ] findPoints( int N) { // Initializing vector of pairs to // store the 4 pairs int [, ] points = new int [4, 2]; // If N is odd, it is impossible // to find 4 such points if (N % 2 == 1) return points; // Initializing a variable // with value equal to N/2 int point = N / 2; // Pushing all the 4 pairs into // vector "points" such that distance // between any two is equal 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; // Returning "points" vector return points; } // Function to print 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]); } } // Driver Code public static void Main() { int N = 6; // Calling the print function print(N); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to find 4 points such that // manhattan distance between // any two of them is equal function findPoints(N) { // Initializing vector of pairs to // store the 4 pairs let points = []; // If N is odd, it is impossible // to find 4 such points if (N % 2 == 1) return points; // Initializing a variable // with value equal to N/2 let point = Math.floor(N / 2); // Pushing all the 4 pairs into // vector "points" such that distance // between any two is equal 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 }); // Returning "points" vector return points; } // Function to print 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>' ); } } // Driver Code let N = 6; // Calling the print function print(N); // This code is contributed by Potta Lokesh </script> |
0, 3 0, -3 3, 0 -3, 0
Time Complexity: O(1)
Auxiliary Space: O(1)
Contact Us