Given two arrays A[] and B[] consisting of X and Y coordinates of N distinct points in a plane, and a positive integer K, the task is to check if there exists any point P in the plane such that the Manhattan distance between the point and all the given points is at most K. If there exists any such point P, then print “Yes”. Otherwise, print “No”.
Examples:
Input: A[] = {1, 0, 2, 1, 1}, B[] = {1, 1, 1, 0, 2}, K = 1
Output: Yes
Explanation:
Consider a point P(1, 1), then the Manhattan distance between P and all the given points are:
- Distance between P and (A[0], B[0]) is |1 – 1| + |1 – 1| = 0.
- Distance between P and (A[1], B[1]) is |1 – 0| + |1 – 1| = 1.
- Distance between P and (A[2], B[2]) is |1 – 2| + |1 – 1| = 1.
- Distance between P and (A[3], B[3]) is |1 – 1| + |1 – 0| = 1.
- Distance between P and (A[4], B[4]) is |1 – 1| + |1 – 2| = 1.
The distance between all the given points and P is at most K(= 1). Therefore, print “Yes”.
Input: A[] = {0, 3, 1}, B[] = {0, 3, 1}, K = 2
Output: No
Approach: The given problem can be solved by finding the Manhattan distance between every pair of N given points. After checking for all pairs of points, if the count of the distance between pairs of points is at most K, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach :
C++
#include<bits/stdc++.h>
using namespace std;
string find( int a[], int b[], int N, int K)
{
for ( int i = 0; i < N; i++)
{
int count = 0;
for ( int j = 0; j < N; j++)
{
if (i == j)
{
continue ;
}
long long int dis = abs (a[i] - a[j]) +
abs (b[i] - b[j]);
if (dis <= K)
{
count++;
}
if (count == N - 1)
{
return "Yes" ;
}
}
}
return "No" ;
}
int main()
{
int N = 5;
int A[] = { 1, 0, 2, 1, 1 };
int B[] = { 1, 1, 1, 0, 2 };
int K = 1;
cout << find(A, B, N, K) << endl;
}
|
Java
import java.io.*;
class GFG {
public static String find(
int [] a, int [] b, int N, int K)
{
for ( int i = 0 ; i < N; i++) {
int count = 0 ;
for ( int j = 0 ; j < N; j++) {
if (i == j) {
continue ;
}
long dis = Math.abs(a[i] - a[j])
+ Math.abs(b[i] - b[j]);
if (dis <= K) {
count++;
}
if (count == N - 1 ) {
return "Yes" ;
}
}
}
return "No" ;
}
public static void main(String[] args)
{
int N = 5 ;
int [] A = { 1 , 0 , 2 , 1 , 1 };
int [] B = { 1 , 1 , 1 , 0 , 2 };
int K = 1 ;
System.out.println(
find(A, B, N, K));
}
}
|
Python3
def find(a, b, N, K):
for i in range (N):
count = 0
for j in range (N):
if (i = = j):
continue
dis = abs (a[i] - a[j]) + abs (b[i] - b[j])
if (dis < = K):
count = count + 1
if (count = = N - 1 ):
return "Yes"
return "No"
N = 5
A = [ 1 , 0 , 2 , 1 , 1 ]
B = [ 1 , 1 , 1 , 0 , 2 ]
K = 1
print (find(A, B, N, K))
|
C#
using System;
class GFG{
public static String find( int [] a, int [] b,
int N, int K)
{
for ( int i = 0; i < N; i++)
{
int count = 0;
for ( int j = 0; j < N; j++)
{
if (i == j)
{
continue ;
}
long dis = Math.Abs(a[i] - a[j]) +
Math.Abs(b[i] - b[j]);
if (dis <= K)
{
count++;
}
if (count == N - 1)
{
return "Yes" ;
}
}
}
return "No" ;
}
public static void Main( string [] args)
{
int N = 5;
int [] A = { 1, 0, 2, 1, 1 };
int [] B = { 1, 1, 1, 0, 2 };
int K = 1;
Console.WriteLine(find(A, B, N, K));
}
}
|
Javascript
<script>
function find(a, b, N, K) {
for (let i = 0; i < N; i++) {
let count = 0;
for (let j = 0; j < N; j++) {
if (i == j) {
continue ;
}
let dis = Math.abs(a[i] - a[j]) +
Math.abs(b[i] - b[j]);
if (dis <= K) {
count++;
}
if (count == N - 1) {
return "Yes" ;
}
}
}
return "No" ;
}
let N = 5;
let A = [ 1, 0, 2, 1, 1 ];
let B = [ 1, 1, 1, 0, 2 ];
let K = 1;
document.write(find(A, B, N, K));
</script>
|
Time Complexity: O(N2)
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 :
22 Apr, 2021
Like Article
Save Article