# Check if given point lies in range of any of the given towers

• Last Updated : 22 Oct, 2021

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, arr) = 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, arr) = 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, arr) = 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] – x)2 + (arr[i] – Y)2) is greater than arr[i] or not. If found to be true, then print True.
• Otherwise, print False.

## C++

 `// C++ program to implement` `// the above approach`   `#include ` `using` `namespace` `std;`   `// Function to check if the point (X, Y)` `// exists in the towers network-range or not` `bool` `checkPointRange(``int` `arr[], ``int` `X,` `                     ``int` `Y, ``int` `N)` `{`   `    ``// Traverse the array arr[]` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Stores distance of the` `        ``// point (X, Y) from i-th tower` `        ``double` `dist` `            ``= ``sqrt``((arr[i] - X) * (arr[i] - X)` `                   ``+ (arr[i] - Y) * (arr[i] - Y));`   `        ``// If dist lies within the` `        ``// range of the i-th tower` `        ``if` `(dist <= arr[i]) {` `            ``return` `true``;` `        ``}` `    ``}`   `    ``// If the point (X, Y) does not lie` `    ``// in the range of any of the towers` `    ``return` `false``;` `}`   `// Driver Code` `int` `main()` `{`   `    ``int` `arr[] = { { 1, 1, 3 }, ` `                     ``{ 10, 10, 3 }, ` `                     ``{ 15, 15, 15 } };` `    ``int` `X = 5, Y = 5;`   `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);`   `    ``// If point (X, Y) lies in the` `    ``// range of any of the towers` `    ``if` `(checkPointRange(arr, X, Y, N)) {` `        ``cout << ``"True"``;` `    ``}` `    ``// Otherwise` `    ``else` `{` `        ``cout << ``"False"``;` `    ``}` `}`

## Java

 `// Java program to implement` `// the above approach` `import` `java.util.*;`   `class` `GFG{` ` `  `// Function to check if the point (X, Y)` `// exists in the towers network-range or not` `static` `boolean` `checkPointRange(``int` `arr[][], ``int` `X,` `                               ``int` `Y, ``int` `N)` `{` `    `  `    ``// Traverse the array arr[]` `    ``for``(``int` `i = ``0``; i < N; i++) ` `    ``{` `        `  `        ``// Stores distance of the` `        ``// point (X, Y) from i-th tower` `        ``double` `dist = Math.sqrt((arr[i][``0``] - X) * ` `                                ``(arr[i][``0``] - X) +` `                                ``(arr[i][``1``] - Y) * ` `                                ``(arr[i][``1``] - Y));` ` `  `        ``// If dist lies within the` `        ``// range of the i-th tower` `        ``if` `(dist <= arr[i][``2``]) ` `        ``{` `            ``return` `true``;` `        ``}` `    ``}` ` `  `    ``// If the point (X, Y) does not lie` `    ``// in the range of any of the towers` `    ``return` `false``;` `}` ` `  `// Driver Code` `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 point (X, Y) lies in the` `    ``// range of any of the towers` `    ``if` `(checkPointRange(arr, X, Y, N))` `    ``{` `        ``System.out.print(``"True"``);` `    ``}` `    `  `    ``// Otherwise` `    ``else` `    ``{` `        ``System.out.print(``"False"``);` `    ``}` `}` `}`   `// This code is contributed by code_hunt`

## Python3

 `# Python3 program to implement` `# the above approach` `from` `math ``import` `sqrt`   `# Function to check if the point (X, Y)` `# exists in the towers network-range or not` `def` `checkPointRange(arr, X, Y, N):` `    `  `    ``# Traverse the array arr[]` `    ``for` `i ``in` `range``(N):` `        `  `        ``# Stores distance of the` `        ``# point (X, Y) from i-th tower` `        ``dist ``=` `sqrt((arr[i][``0``] ``-` `X) ``*` `                    ``(arr[i][``0``] ``-` `X) ``+` `                    ``(arr[i][``1``] ``-` `Y) ``*` `                    ``(arr[i][``1``] ``-` `Y))`   `        ``# If dist lies within the` `        ``# range of the i-th tower` `        ``if` `(dist <``=` `arr[i][``2``]):` `            ``return` `True`   `    ``# If the point (X, Y) does not lie` `    ``# in the range of any of the towers` `    ``return` `False`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``arr ``=` `[ [ ``1``, ``1``, ``3` `], ` `            ``[ ``10``, ``10``, ``3` `],` `            ``[ ``15``, ``15``, ``15` `] ]` `    ``X ``=` `5` `    ``Y ``=` `5`   `    ``N ``=`  `len``(arr)`   `    ``# If point (X, Y) lies in the` `    ``# range of any of the towers` `    ``if` `(checkPointRange(arr, X, Y, N)):` `        ``print``(``"True"``)` `        `  `    ``# Otherwise` `    ``else``:` `        ``print``(``"False"``)`   `# This code is contributed by bgangwar59`

## C#

 `// C# program to implement` `// the above approach  ` `using` `System;` `  `  `class` `GFG{` `  `  `// Function to check if the point (X, Y)` `// exists in the towers network-range or not` `static` `bool` `checkPointRange(``int``[,] arr, ``int` `X,` `                            ``int` `Y, ``int` `N)` `{` `    `  `    ``// Traverse the array arr[]` `    ``for``(``int` `i = 0; i < N; i++) ` `    ``{` `        `  `        ``// Stores distance of the` `        ``// point (X, Y) from i-th tower` `        ``double` `dist = Math.Sqrt((arr[i, 0] - X) * ` `                                ``(arr[i, 0] - X) +` `                                ``(arr[i, 1] - Y) * ` `                                ``(arr[i, 1] - Y));` `  `  `        ``// If dist lies within the` `        ``// range of the i-th tower` `        ``if` `(dist <= arr[i, 2]) ` `        ``{` `            ``return` `true``;` `        ``}` `    ``}` `  `  `    ``// If the point (X, Y) does not lie` `    ``// in the range of any of the towers` `    ``return` `false``;` `}` `  `  `// Driver Code` `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 point (X, Y) lies in the` `    ``// range of any of the towers` `    ``if` `(checkPointRange(arr, X, Y, N))` `    ``{` `        ``Console.Write(``"True"``);` `    ``}` `     `  `    ``// Otherwise` `    ``else` `    ``{` `        ``Console.Write(``"False"``);` `    ``}` `}` `}` `  `  `// This code is contributed by susmitakundugoaldanga`

## Javascript

 ``

Output:

`True`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :