Check if any point exists in a plane whose Manhattan distance is at most K from N given points | C ++ program for the above approach ; Function to check if there exists any point with at most K distance from N given points ; Traverse the given N points ; Stores the count of pairs of coordinates having Manhattan distance <= K ; For the same coordinate ; Calculate Manhattan distance ; If Manhattan distance <= K ; If all coordinates can meet ; If all coordinates can 't meet ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }