Skip to content

Commit 6bec9ff

Browse files
Merge pull request #158 from sachin4429/master
1828. Queries on Number of Points Inside a Circle
2 parents e85ac05 + 03f0bb8 commit 6bec9ff

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

LeetCode/PointInsideCircle.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates.
3+
4+
You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj.
5+
6+
For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside.
7+
8+
Return an array answer, where answer[j] is the answer to the jth query.
9+
10+
Constraints:
11+
12+
1 <= points.length <= 500
13+
points[i].length == 2
14+
0 <= x??????i, y??????i <= 500
15+
1 <= queries.length <= 500
16+
queries[j].length == 3
17+
0 <= xj, yj <= 500
18+
1 <= rj <= 500
19+
All coordinates are integers.
20+
21+
*/
22+
class PointInsideCircle {
23+
double equation(int arr[], int brr[]) //function taht take points and circle constants as input and return Decimal values.
24+
{
25+
return Math.pow((brr[0]-arr[0]),2) + Math.pow((brr[1]-arr[1]),2) - Math.pow(arr[2],2); //returning value after putting the points on circle
26+
}
27+
public int[] countPoints(int[][] points, int[][] queries) { //Given function
28+
int res[] = new int[queries.length]; //Array res of type integer
29+
for(int i = 0; i<queries.length; i++) //for loop that itrates over queries (circle)
30+
{
31+
int count = 0; //count variable of type int
32+
for(int j = 0; j<points.length; j++) //for loop that itrates over all given points
33+
{
34+
if(equation(queries[i],points[j]) <= 0) //checking that the value is less than or 0 after putting the value in eqation
35+
count++; //incrementing the value of count
36+
}
37+
res[i] = count; //storing the value of count in res Array
38+
}
39+
return res; //returning result
40+
}
41+
}

0 commit comments

Comments
 (0)