Given n coordinate (x, y) of points on 2D plane and Q queries. Each query contains an integer r, the task is to count the number of points lying inside or on the circumference of the circle having radius r and centered at the origin.
Input : n = 5 Coordinates: 1 1 2 2 3 3 -1 -1 4 4 Query 1: 3 Query 2: 32 Output : 3 5 For first query radius = 3, number of points lie inside or on the circumference are (1, 1), (-1, -1), (2, 2). There are only 3 points lie inside or on the circumference of the circle. For second query radius = 32, all five points are inside the circle.
The equation for the circle centered at origin (0, 0) with radius r, x2 + y2 = r2. And condition for a point at (x1, y1) to lie inside or on the circumference, x12 + y12 <= r2.
A Naive approach can be for each query, traverse through all points and check the condition. This take O(n*Q) time complexity.
An Efficient approach is to precompute x2 + y2 for each point coordinate and store them in an array p. Now, sort the array p. Then apply binary search on the array to find last index with condition p[i] <= r2 for each query.
Below is the implementation of this approach:
Time Complexity: O(n log n) for preprocessing and O(Q Log n) for Q queries.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Count Integral points inside a Triangle
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Coordinates of rectangle with given points lie inside
- Find if a point lies inside, outside or on the circumcircle of three points A, B, C
- Check if the given point lies inside given N points of a Convex Polygon
- Find minimum radius such that atleast k point lie inside the circle
- Circle and Lattice Points
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check whether it is possible to join two points given on circle such that distance between them is k
- Non-crossing lines to connect points in a circle
- Angular Sweep (Maximum points that can be enclosed in a circle of given radius)
- Number of quadrilateral formed with N distinct points on circumference of Circle
- Queries to check if it is possible to join boxes in a circle
- Count of integral coordinates that lies inside a Square
- Count maximum points on same line
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Count of different straight lines with total n points with m collinear
- Count ways to divide circle using N non-intersecting chords