Queries on count of points lie inside a circle in C++


In this problem, we are given n points that lie of a 2D plane, each coordinate is (x,y). Our task is two solve queries. For each query, we are given an integer R. We need to find the count of points lying inside the circle, taking the circle’s center at origin and radius R.

Problem description

For each query, we need to find the total number of points out of n points that lie inside the circle (i.e. inside the circumference) of radius R and center point origin (0, 0).

Let’s take an example to understand the problem better

Input

n = 4
2 1
1 2
3 3
-1 0
-2 -2
Query 1: 2

Output

1

Explanation − For our query, the radius is 2, the point -1 0, lie inside the circle, and all the other lie outside it.

The mathematical equation of the circle is, (x2 - x1)2 + (x2 - x1)2 = r2. So, for a point to lie inside the circle whose center is (0,0). The point (x,y) must satisfy x2 + y2 <= r2.

To solve this problem, a simple approach will be to traverse all points for each query and check if it lies inside the circumference of the circle or not using the formula.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;

int solveQuery(int x[], int y[], int n, int R) {
   int count = 0;
   for(int i = 0; i< n ; i++){
      if(((x[i]*x[i]) + (y[i]*y[i]) ) <= (R*R) )
      count++;
   }
   return count;
}
int main() {

   int x[] = { 2, 1, 3, -1, -2 };
   int y[] = { 1, 2, 3, 0, -2 };
   int n = sizeof(x) / sizeof(x[0]);
   int Q = 2;
   int query[] = {4, 2 };

   for(int i = 0; i < Q; i++)
   cout<<"For Query "<<(i+1)<<": The number of points that lie inside the circle is "<<solveQuery(x,    y, n, query[i])<<"\n";
   return 0;
}

Output

For Query 1: The number of points that lie inside the circle is 4
For Query 2: The number of points that lie inside the circle is 1

The solution to the problem using this approach will have time complexity of O(n*Q). Because for each query, we will compute the value of x2 + y2, for all n points.

So, an efficient solution will be by precomputing the value of x2 + y2, for all n points. And storing it to an array which can be used for all queries. And then find the solution for each query. For further optimizing the program, we can sort the array and then find the first element which lies out of the circle. To improve the time taken.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;

int solveQuery(int points[], int n, int rad) {

   int l = 0, r = n - 1;
   while ((r - l) > 1) {
      int mid = (l + r) / 2;
      if (points[mid] > (rad * rad))
      r = mid - 1;
      else
      l = mid;
   }
   if ((sqrt(points[l])) > (rad * 1.0))
   return 0;
   else if ((sqrt(points[r])) <= (rad * 1.0))
   return r + 1;
   else
   return l + 1;
}

int main() {

   int n = 5;
   int point[n][2] = { {2, 1}, {1, 2}, {3, 3}, {-1, 0}, {-2, -2} };
   int Q = 2;
   int query[] = {4, 2 };
   int points[n];
   // Precomputing Values
   for (int i = 0; i < n; i++)
   points[i] = ( point[i][0]*point[i][0] ) + ( point[i][1]*point[i][1] );
   sort(points, points + n);
   for(int i = 0; i < Q; i++)
   cout<<"For Query "<<(i+1)<<": The number of points that lie inside the circle is "         <<solveQuery(points, n, query[i])<<"\n";
   return 0;
}

Output

For Query 1: The number of points that lie inside the circle is 4
For Query 2: The number of points that lie inside the circle is 1

Updated on: 09-Sep-2020

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements