The Wayback Machine - https://web.archive.org/web/20240823163312/https://www.geeksforgeeks.org/check-if-given-point-lies-in-range-of-any-of-the-given-towers/
Open In App

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

Last Updated : 01 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Medium
15.06%
5.8L

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++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the point (X, Y)
// exists in the towers network-range or not
bool checkPointRange(int arr[][3], 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][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
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 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




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to check if the point (X, Y)
// exists in the towers network-range or not
function checkPointRange(arr, X, Y, N)
{
      
    // Traverse the array arr[]
    for(let i = 0; i < N; i++)
    {
          
        // Stores distance of the
        // point (X, Y) from i-th tower
        let 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
 
    let arr = [[ 1, 1, 3 ],
               [ 10, 10, 3 ],
              [ 15, 15, 15 ]];
    let X = 5, Y = 5;
   
    let N = arr.length;
   
    // If point (X, Y) lies in the
    // range of any of the towers
    if (checkPointRange(arr, X, Y, N))
    {
        document.write("True");
    }
      
    // Otherwise
    else
    {
        document.write("False");
    }
           
</script>


Output

True

Time Complexity: O(N * log(N)), in-built sqrt function has log(N) time complexity.
Auxiliary Space: O(1)



Similar Reads

Minimum number of towers required such that every house is in the range of at least one tower
Given a map of the city and the network range, the task is to determine the minimum number of the tower so that every house is within range of at least one tower. Each tower must be installed on top of an existing house. Examples: Input: range : 1 house : 1 2 3 4 5 Output: 2 Input: range : 2 house : 7 2 4 6 5 9 12 11 Output: 3 All cities can be cov
7 min read
Minimum broadcast range required by M towers to reach N houses
Given an array a containing positions of N houses, and an array b containing positions of M radio towers, each placed along a horizontal line, the task is to find the minimum broadcast range such that each radio tower reaches every house.Examples: Input: a[] = {1, 5, 11, 20}, b[] = {4, 8, 15} Output: 5 Explanation: The minimum range required is 5,
8 min read
Check if the given point lies inside given N points of a Convex Polygon
Given coordinates of the N points of a Convex Polygon. The task is to check if the given point (X, Y) lies inside the polygon. Examples: Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)}, Query: X = 3, Y = 2 Below is the image of plotting of the given points: Output: YES Input: N = 7, Points: {(1, 1), (2, 1), (3, 1), (4
15 min read
Check whether a given point lies on or inside the rectangle | Set 3
Given two numbers a and b where b &lt; a form a rectangle with points (0, b), (b, 0), (a-b, b), (b, a-b). Given a point (x, y), the task is to check whether this point lies inside or on the rectangle or not. Examples: Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Give
6 min read
Check if given point lies on the Square Spiral Path
Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.Move three units left side from the current positio
6 min read
Check whether a given point lies inside a triangle or not
Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not. Example: Input: A = (0, 0), B = (10, 30), C = (20, 0), P(10, 15)Output: InsideExplanation: B(10,30) / \ / \ / \ / P \ P' / \ A(0,0) ----------- C(20,0) Input: A = (0, 0), B = (10, 30), C = (20, 0), P(30, 15)Output: Out
15+ min read
Check whether the point (x, y) lies on a given line
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line. Examples: Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line's equation Input: m = 5, c = 2, x = 2, y = 5 Output: No A
4 min read
How to check if a given point lies inside or outside a polygon?
Given a polygon and a point 'p', find if 'p' lies inside the polygon or not. The points lying on the border are considered inside. Examples: Recommended ProblemPlease solve it on PRACTICE first, before moving on to the solution Solve ProblemApproach: The idea to solve this problem is based on How to check if two given line segments intersect, and t
9 min read
Check whether a given point lies inside a rectangle or not
Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:   Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration : Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20, 20)Output : noIllustration : Prerequisite: Che
14 min read
Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.Examples: Input : Centre(0, 0, 0) Radius 3 Point(1, 1, 1) Output :Point is inside the sphere Input :Centre(0, 0, 0) Radius 3 Point(2, 1, 2) Output :Point lies on the sphere Input :Centre(0
7 min read
Check if a point lies inside a rectangle | Set-2
Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is already discussed in a previous post. In this pos
6 min read
Generate a pair of integers from a range [L, R] whose LCM also lies within the range
Given two integers L and R, the task is to find a pair of integers from the range [L, R] having LCM within the range [L, R] as well. If no such pair can be obtained, then print -1. If multiple pairs exist, print any one of them. Examples: Input: L = 13, R = 69Output: X =13, Y = 26Explanation: LCM(x, y) = 26 which satisfies the conditions L ? x &lt;
4 min read
Find a point that lies inside exactly K given squares
Given an integer K and an array arr each of whose element x represents a square with two of its vertices as (0, 0) and (x, x). The task is to find a point which lies in exactly K squares. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: (3, 3) The point (3, 3) lies inside 3rd and 4th square only. Input: arr[] = {8, 1, 55, 90}, K = 3 Output: (8,
3 min read
Largest area in a grid unbounded by towers
Given two integers L and W representing the dimensions of a grid, and two arrays X[] and Y[] of length N denoting the number of towers present on the grid at positions (X[i], Y[i]), where (0 &lt;= i &lt;= N - 1). The task is to find the largest unbounded area in the field which is not defended by the towers. Examples: Input: L = 15, W = 8, N = 3 X[
8 min read
CSES Solutions - Towers
You are given N cubes as an array cubes[] in a certain order, and your task is to build towers using them. Whenever two cubes are one on top of the other, the upper cube must be smaller than the lower cube. You must process the cubes in the given order. You can always either place the cube on top of an existing tower or begin a new tower. What is t
6 min read
Check if any K ranges overlap at any point
Given N ranges [L, R] and an integer K, the task is to check if there are any K ranges that overlap at any point. Examples: Input: ranges[][] = {{1, 3}, {2, 4}, {3, 4}, {7, 10}}, K = 3 Output: Yes 3 is a common point among the ranges {1, 3}, {2, 4} and {3, 4}. Input: ranges[][] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}, K = 2 Output: No Recommended: Pleas
13 min read
Find if a point lies inside a Circle
Given a circle (coordinates of centre and radius) and a point (coordinate), find if the point lies inside or on the circle, or not. Examples : Input: x = 4, y = 4 // Given Point circle_x = 1, circle_y = 1, rad = 6; // Circle Output: Inside Input: x = 3, y = 3 // Given Point circle_x = 0, circle_y = 1, rad = 2; // Circle Output: Outside We strongly
5 min read
Find if a point lies inside, outside or on the circumcircle of three points A, B, C
Given four non-collinear points A, B, C, and P. The task is to find whether point P lies inside, outside or on the circumcircle formed by points A, B, and C.Examples Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {2, 4}; Output: Point (2, 4) is inside the circumcircle. Input: A = {2, 8}, B = {2, 1}, C = {4, 5}, P = {3, 0}; Output: Point (3, 0) lies
15+ min read
Rearrange two given arrays such that sum of same indexed elements lies within given range
Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print "Yes". Otherwise, print "No". Examples: Input: arr
6 min read
Count pairs from a given array whose product lies in a given range
Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i &lt; j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: Valid pairs are {4, 1}, {1, 5} and {4, 2}. Input: a
13 min read
Find the GCD that lies in given range
Given two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 5] and is common divisor of 9 and 27. Input : a =
7 min read
Maximum time such that absolute difference between hour and minute lies in given range
Given a 24-hour time value, where on some numbers are question marks( '?' ), and two integers L and R. The question mark can be replaced with any number. The task is to find the maximum time such that the absolute difference between hour value and minute value lies in the interval [L, R]. If no possible value of time exists, then print -1. Examples
13 min read
Find two distinct numbers such that their LCM lies in given range
Given two numbers L and R, the task is to find two distinct minimum positive integers X and Y such that whose LCM lies in the range [L, R]. If there doesn't exist any value of X and Y then print "-1". Examples: Input: L = 3, R = 8 Output: x = 3, y=6Explanation:LCM of 3 and 6 is 6 which is in range 3, 8 Input: L = 88, R = 90Output: -1Explanation:Min
4 min read
Maximum cost of a value over the range [1, N] such that values lies in at most K given ranges
Given a positive integer N and an array arr[] of size M of the type {L, R, C} such that C is the cost of choosing an element in the range [L, R] and a positive integer K, the task is to find the maximum cost of a value over the range [1, N] such that values lies in at most K given range arr[]. Examples: Input: arr[] = {{2, 8, 800}, {6, 9, 1500}, {4
8 min read
Remove elements from the array whose frequency lies in the range [l, r]
Given an array of integers, remove the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 1 5 We remove all those elements with frequencies from 2 to 5. Input : arr[] = { 1, 2, 3, 3, 3, 3 } l = 2, r = 3 Output : 3 3 3 3 Approach : Take a hash map, which will stor
5 min read
Check if a given circle lies completely inside the ring formed by two concentric circles
Given two circles of radius r and R, both have their centre at the origin. Now, given another circle of radius r1 and centre at (x1, y1). Check, if the third circle(circle of radius r1) lies completely inside the ring formed by two circles of radius r and R.Examples : Input : r = 8 R = 4 r1 = 2 x1 = 6 y1 = 0 Output : yes Input : r = 8 R = 4 r1 = 2
6 min read
Check if vertex X lies in subgraph of vertex Y for the given Graph
Given an undirected graph and two vertices X and Y, our task is to check whether the vertex X lies in the subgraph of the vertex Y. Examples: Input: X = 2, Y = 3 Output: No Explanation: Subgraph of a vertex Y = 3 is set of all the vertex which lies below Y and are reachable by Y. Here subgraph of 3 contains {6} not 2. Input: X = 6, Y = 1 Output: Ye
10 min read
Check if any point overlaps the given Circle and Rectangle
Given two opposite diagonal points of a rectangle (X1, Y1), (X2, Y2) and the center, radius of the circle R, (Xc, Yc), the task is to check if there exists any point P that belongs to both the circle as well as the rectangle. Examples: Input: R = 2, Xc = 0, Yc = 0, X1 = 1, Y1 = 0, X2 = 3, Y2 = 3 Output: true Explanation: Clearly, from the below ill
7 min read
Check if any point exists in a plane whose Manhattan distance is at most K from N given points
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". E
7 min read
Check if it is possible to reach any point on the circumference of a given circle from origin
Given a string S representing a sequence of moves(L, R, U, and D) and an integer R representing the radius of a circle whose center is the origin (0, 0), the task is to check if it is possible to reach any point on the circumference of the given circle from the origin by choosing any subsequence of moves from the string S. If it is possible to reac
10 min read