Check if given point lies in range of any of the given towers
Last Updated :
01 Sep, 2022
Given a 2D array arr[][] consisting of N rows of the form {Xi, Yi, Ri} such that (Xi, Yi) represents the position of a tower and Ri represents the network range of that tower. Given two integers X and Y, the task is to check if the point (X, Y) lies in the network range of the towers or not.
Examples:
Input: arr[][] = { {1, 1, 3}, {10, 10, 5}, {15, 15, 15} }, X = 5, Y = 5
Output: True
Explanation:
Distance of point(5, 5) from (arr[0][0], arr[0][1]) = 5.65685 and
the range of first tower is 3. Therefore, the point(X, Y) does not lie
in the network-range of the first tower.
Distance of point(5, 5) from (arr[1][0], arr[1][1]) = 7.07107 and
the range of second tower is 5. Therefore, the point(X, Y) does not lie
in the network-range of the second tower.
Distance of point(5, 5) from (arr[2][0], arr[2][1]) = 14.1421 and
the range of third tower is 15. Therefore, the point(X, Y) lies
in the network-range of the third tower.
Since, point (X, Y) lies in the range of the third tower.
Therefore, the required output is True.
Input: arr[][] = { {1, 1, 3}, {10, 10, 3}, {15, 15, 3} }, X = 5, Y = 5
Output: False
Approach: Follow the steps given below to solve the problem:
- Traverse the array and for each row ( tower ) traversed,
- Check if the value of sqrt((arr[i][0] – x)2 + (arr[i][1] – Y)2) is greater than arr[i][2] or not.
- If found to be true, then print True.
- Otherwise, print False.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPointRange( int arr[][3], int X,
int Y, int N)
{
for ( int i = 0; i < N; i++) {
double dist
= sqrt ((arr[i][0] - X) * (arr[i][0] - X)
+ (arr[i][1] - Y) * (arr[i][1] - Y));
if (dist <= arr[i][2]) {
return true ;
}
}
return false ;
}
int main()
{
int arr[][3] = { { 1, 1, 3 },
{ 10, 10, 3 },
{ 15, 15, 15 } };
int X = 5, Y = 5;
int N = sizeof (arr) / sizeof (arr[0]);
if (checkPointRange(arr, X, Y, N)) {
cout << "True" ;
}
else {
cout << "False" ;
}
}
|
Java
import java.util.*;
class GFG{
static boolean checkPointRange( int arr[][], int X,
int Y, int N)
{
for ( int i = 0 ; i < N; i++)
{
double dist = Math.sqrt((arr[i][ 0 ] - X) *
(arr[i][ 0 ] - X) +
(arr[i][ 1 ] - Y) *
(arr[i][ 1 ] - Y));
if (dist <= arr[i][ 2 ])
{
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int arr[][] = { { 1 , 1 , 3 },
{ 10 , 10 , 3 },
{ 15 , 15 , 15 } };
int X = 5 , Y = 5 ;
int N = arr.length;
if (checkPointRange(arr, X, Y, N))
{
System.out.print( "True" );
}
else
{
System.out.print( "False" );
}
}
}
|
Python3
from math import sqrt
def checkPointRange(arr, X, Y, N):
for i in range (N):
dist = sqrt((arr[i][ 0 ] - X) *
(arr[i][ 0 ] - X) +
(arr[i][ 1 ] - Y) *
(arr[i][ 1 ] - Y))
if (dist < = arr[i][ 2 ]):
return True
return False
if __name__ = = '__main__' :
arr = [ [ 1 , 1 , 3 ],
[ 10 , 10 , 3 ],
[ 15 , 15 , 15 ] ]
X = 5
Y = 5
N = len (arr)
if (checkPointRange(arr, X, Y, N)):
print ( "True" )
else :
print ( "False" )
|
C#
using System;
class GFG{
static bool checkPointRange( int [,] arr, int X,
int Y, int N)
{
for ( int i = 0; i < N; i++)
{
double dist = Math.Sqrt((arr[i, 0] - X) *
(arr[i, 0] - X) +
(arr[i, 1] - Y) *
(arr[i, 1] - Y));
if (dist <= arr[i, 2])
{
return true ;
}
}
return false ;
}
public static void Main()
{
int [,] arr = { { 1, 1, 3 },
{ 10, 10, 3 },
{ 15, 15, 15 } };
int X = 5, Y = 5;
int N = arr.Length;
if (checkPointRange(arr, X, Y, N))
{
Console.Write( "True" );
}
else
{
Console.Write( "False" );
}
}
}
|
Javascript
<script>
function checkPointRange(arr, X, Y, N)
{
for (let i = 0; i < N; i++)
{
let dist = Math.sqrt((arr[i][0] - X) *
(arr[i][0] - X) +
(arr[i][1] - Y) *
(arr[i][1] - Y));
if (dist <= arr[i][2])
{
return true ;
}
}
return false ;
}
let arr = [[ 1, 1, 3 ],
[ 10, 10, 3 ],
[ 15, 15, 15 ]];
let X = 5, Y = 5;
let N = arr.length;
if (checkPointRange(arr, X, Y, N))
{
document.write( "True" );
}
else
{
document.write( "False" );
}
</script>
|
Time Complexity: O(N * log(N)), in-built sqrt function has log(N) time complexity.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...