Open In App

# Find minimum radius such that atleast k point lie inside the circle

Given a positive integer K, a circle center at (0, 0) and coordinates of some points. The task is to find minimum radius of the circle so that at-least k points lie inside the circle. Output the square of the minimum radius.

Examples:

`Input : (1, 1), (-1, -1), (1, -1),          k = 3Output : 2We need a circle of radius at least 2to include 3 points.Input : (1, 1), (0, 1), (1, -1),          k = 2Output : 1We need a circle of radius at least 1to include 2 points. The circle around(0, 0) of radius 1 would include (1, 1)and (0, 1).`

The idea is to find square of Euclidean Distance of each point from origin (0, 0). Now, sort these distance in increasing order. Now the kth element of distance is the required minimum radius.
Below is the implementation of this approach:

## C++

 `// C++ program to find minimum radius``// such that atleast k point lie inside``// the circle``#include``using` `namespace` `std;` `// Return minimum distance required so that``// atleast k point lie inside the circle.``int` `minRadius(``int` `k, ``int` `x[], ``int` `y[], ``int` `n)``{``   ``int` `dis[n];``    ` `   ``// Finding distance between of each``   ``// point from origin``   ``for` `(``int` `i = 0; i < n; i++)``       ``dis[i] = x[i] * x[i] + y[i] * y[i];``    ` `    ``// Sorting the distance``    ``sort(dis, dis + n);``    ` `    ``return` `dis[k - 1];``}` `// Driven Program``int` `main()``{``  ``int` `k = 3;``  ``int` `x[] = { 1, -1, 1 };``  ``int` `y[] = { 1, -1, -1 };``  ``int` `n = ``sizeof``(x)/``sizeof``(x);``    ` `  ``cout << minRadius(k, x, y, n) << endl;``    ` `  ``return` `0;``}`

## Java

 `// Java program to find minimum radius``// such that atleast k point lie inside``// the circle``import` `java.util.Arrays;` `class` `GFG``{` `    ``// Return minimum distance required so that``    ``// atleast k point lie inside the circle.``    ``static` `int` `minRadius(``int` `k, ``int``[] x, ``int``[] y,``                                          ``int` `n)``    ``{``        ``int``[] dis=``new` `int``[n];``    ` `        ``// Finding distance between of each``        ``// point from origin``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``dis[i] = x[i] * x[i] + y[i] * y[i];``    ` `        ``// Sorting the distance``        ``Arrays.sort(dis);``    ` `        ``return` `dis[k - ``1``];``    ``}` `    ``// Driven Program``    ``public` `static` `void` `main (String[] args) {``        ` `    ``int` `k = ``3``;``    ``int``[] x = { ``1``, -``1``, ``1` `};``    ``int``[] y = { ``1``, -``1``, -``1` `};``    ``int` `n = x.length;``    ` `    ``System.out.println(minRadius(k, x, y, n));` `    ``}``}` `/* This code is contributed by Mr. Somesh Awasthi */`

## Python3

 `# Python3 program to find minimum radius``# such that atleast k point lie inside``# the circle`  `# Return minimum distance required so``# that atleast k point lie inside the``# circle.``def` `minRadius(k, x, y, n):``    ``dis ``=` `[``0``] ``*` `n` `    ``# Finding distance between of each``    ``# point from origin` `    ``for` `i ``in` `range``(``0``, n):``        ``dis[i] ``=` `x[i] ``*` `x[i] ``+` `y[i] ``*` `y[i]` `    ``# Sorting the distance``    ``dis.sort()` `    ``return` `dis[k ``-` `1``]``        ` `# Driver Program``k ``=` `3``x ``=` `[``1``, ``-``1``, ``1``]``y ``=` `[``1``, ``-``1``, ``-``1``]``n ``=` `len``(x)` `print``(minRadius(k, x, y, n))` `# This code is contributed by``# Prasad Kshirsagar`

## C#

 `// C# program to find minimum radius``// such that atleast k point lie inside``// the circle``using` `System;` `class` `GFG {` `    ``// Return minimum distance required``    ``// so that atleast k point lie inside``    ``// the circle.``    ``static` `int` `minRadius(``int` `k, ``int` `[]x,``                          ``int``[] y, ``int` `n)``    ``{``        ``int``[] dis = ``new` `int``[n];``    ` `        ``// Finding distance between of``        ``// each point from origin``        ``for` `(``int` `i = 0; i < n; i++)``            ``dis[i] = x[i] * x[i] +``                       ``y[i] * y[i];``    ` `        ``// Sorting the distance``        ``Array.Sort(dis);``    ` `        ``return` `dis[k - 1];``    ``}` `    ``// Driven Program``    ``public` `static` `void` `Main ()``    ``{``        ``int` `k = 3;``        ``int``[] x = { 1, -1, 1 };``        ``int``[] y = { 1, -1, -1 };``        ``int` `n = x.Length;``        ` `        ``Console.WriteLine(``              ``minRadius(k, x, y, n));``    ``}``}` `// This code is contributed by vt_m.`

## Javascript

 `   `

## PHP

 ``

Output

```2
```

Time complexity: O(n + nlogn)
Auxiliary Space: O(n)ve.

#### Approach#2: Using binary search

This code uses binary search to find the minimum radius such that at least k points lie inside or on the circumference of the circle. It first finds the maximum distance between any two points, then performs binary search on the range [0, max_distance] to find the minimum radius.

#### Algorithm

1. Initialize left = 0 and right = maximum distance between any two points in the given set of points.
2. While left <= right, find mid = (left + right) / 2
3. Check if there exist k points inside or on the circumference of a circle with radius mid using a simple linear search. 4. If k or more points are inside or on the circumference of the circle, set right = mid – 1.
5. If less than k points are inside or on the circumference of the circle, set left = mid + 1.
6. After the binary search, the value of left will be the minimum radius required to include k points.

## C++

 `#include ``#include ``#include ` `using` `namespace` `std;` `double` `dist(pair<``int``, ``int``> p1, pair<``int``, ``int``> p2)``{``    ``// Calculate Euclidean distance between two points``    ``return` `sqrt``(``pow``(p1.first - p2.first, 2)``                ``+ ``pow``(p1.second - p2.second, 2));``}` `int` `count_points_in_circle(vector > points,``                           ``pair<``int``, ``int``> center,``                           ``double` `radius)``{``    ``// Count the number of points inside or on the``    ``// circumference of the circle``    ``int` `count = 0;``    ``for` `(``auto` `point : points) {``        ``if` `(dist(point, center) <= radius) {``            ``count++;``        ``}``    ``}``    ``return` `count;``}` `int` `MinimumRadius(vector > points, ``int` `k)``{``    ``double` `left = 0.0;``    ``double` `right = 0.0;``    ``// Find the maximum distance between any two points``    ``for` `(``int` `i = 0; i < points.size(); i++) {``        ``for` `(``int` `j = i + 1; j < points.size(); j++) {``            ``double` `d = dist(points[i], points[j]);``            ``if` `(d > right) {``                ``right = d;``            ``}``        ``}``    ``}``    ``while` `(left <= right) {``        ``double` `mid = (left + right) / 2.0;``        ``bool` `found = ``false``;``        ``// Check if there exist k points inside or on the``        ``// circumference of a circle with radius mid``        ``for` `(``int` `i = 0; i < points.size(); i++) {``            ``if` `(count_points_in_circle(points, points[i],``                                       ``mid)``                ``>= k) {``                ``found = ``true``;``                ``break``;``            ``}``        ``}``        ``if` `(found) {``            ``right = mid - 1.0;``        ``}``        ``else` `{``            ``left = mid + 1.0;``        ``}``    ``}``    ``return` `static_cast``<``int``>(left);``}` `// Example usage``int` `main()``{``    ``vector > points{ { 1, 1 },``                                    ``{ -1, -1 },``                                    ``{ 1, -1 } };``    ``int` `k = 3;``    ``cout << MinimumRadius(points, k) << endl;``}`

## Java

 `import` `java.util.ArrayList;` `public` `class` `MinimumRadiusProblem {` `    ``public` `static` `double` `dist(``double``[] p1, ``double``[] p2) {``        ``// Calculate Euclidean distance between two points``        ``return` `Math.sqrt(Math.pow(p1[``0``] - p2[``0``], ``2``) + Math.pow(p1[``1``] - p2[``1``], ``2``));``    ``}` `    ``public` `static` `int` `count_points_in_circle(ArrayList<``double``[]> points, ``double``[] center, ``double` `radius) {``        ``// Count the number of points inside or on the circumference of the circle``        ``int` `count = ``0``;``        ``for` `(``double``[] point : points) {``            ``if` `(dist(point, center) <= radius) {``                ``count++;``            ``}``        ``}``        ``return` `count;``    ``}` `    ``public` `static` `int` `minimumRadius(ArrayList<``double``[]> points, ``int` `k) {``        ``double` `left = ``0.0``;``        ``double` `right = ``0.0``;``        ``// Find the maximum distance between any two points``        ``for` `(``int` `i = ``0``; i < points.size(); i++) {``            ``for` `(``int` `j = i + ``1``; j < points.size(); j++) {``                ``double` `d = dist(points.get(i), points.get(j));``                ``if` `(d > right) {``                    ``right = d;``                ``}``            ``}``        ``}``        ``while` `(left <= right) {``            ``double` `mid = (left + right) / ``2.0``;``            ``boolean` `found = ``false``;``            ``// Check if there exist k points inside or on the circumference of a circle with radius mid``            ``for` `(``double``[] point : points) {``                ``if` `(count_points_in_circle(points, point, mid) >= k) {``                    ``found = ``true``;``                    ``break``;``                ``}``            ``}``            ``if` `(found) {``                ``right = mid - ``1.0``;``            ``} ``else` `{``                ``left = mid + ``1.0``;``            ``}``        ``}``        ``return` `(``int``) Math.floor(left);``    ``}` `    ``// Example usage``    ``public` `static` `void` `main(String[] args) {``        ``ArrayList<``double``[]> points = ``new` `ArrayList<>();``        ``points.add(``new` `double``[]{``1``, ``1``});``        ``points.add(``new` `double``[]{-``1``, -``1``});``        ``points.add(``new` `double``[]{``1``, -``1``});` `        ``int` `k = ``3``;``        ``System.out.println(minimumRadius(points, k));``    ``}``}`

## Python3

 `import` `math` `def` `dist(p1, p2):``    ``# Calculate Euclidean distance between two points``    ``return` `math.sqrt((p1[``0``] ``-` `p2[``0``])``*``*``2` `+` `(p1[``1``] ``-` `p2[``1``])``*``*``2``)` `def` `count_points_in_circle(points, center, radius):``    ``# Count the number of points inside or on the circumference of the circle``    ``count ``=` `0``    ``for` `point ``in` `points:``        ``if` `dist(point, center) <``=` `radius:``            ``count ``+``=` `1``    ``return` `count` `def` `minimum_radius(points, k):``    ``left, right ``=` `0``, ``0``    ``# Find the maximum distance between any two points``    ``for` `i ``in` `range``(``len``(points)):``        ``for` `j ``in` `range``(i``+``1``, ``len``(points)):``            ``d ``=` `dist(points[i], points[j])``            ``if` `d > right:``                ``right ``=` `d``    ``while` `left <``=` `right:``        ``mid ``=` `(left ``+` `right) ``/` `2``        ``found ``=` `False``        ``# Check if there exist k points inside or on the circumference of a circle with radius mid``        ``for` `i ``in` `range``(``len``(points)):``            ``if` `count_points_in_circle(points, points[i], mid) >``=` `k:``                ``found ``=` `True``                ``break``        ``if` `found:``            ``right ``=` `mid ``-` `1``        ``else``:``            ``left ``=` `mid ``+` `1``    ``return` `int``(left)` `# Example usage``points ``=` `[(``1``, ``1``), (``-``1``, ``-``1``), (``1``, ``-``1``)]``k ``=` `3``print``(minimum_radius(points, k))`

## Javascript

 `// JavaScript implementation of Minimum Radius problem``function` `dist(p1, p2) {``    ``// Calculate Euclidean distance between two points``    ``return` `Math.sqrt(Math.pow(p1 - p2, 2) + Math.pow(p1 - p2, 2));``}` `function` `count_points_in_circle(points, center, radius) {``    ``// Count the number of points inside or on the``    ``// circumference of the circle``    ``let count = 0;``    ``for` `(let point of points) {``        ``if` `(dist(point, center) <= radius) {``            ``count++;``        ``}``    ``}``    ``return` `count;``}` `function` `MinimumRadius(points, k) {``    ``let left = 0.0;``    ``let right = 0.0;``    ``// Find the maximum distance between any two points``    ``for` `(let i = 0; i < points.length; i++) {``        ``for` `(let j = i + 1; j < points.length; j++) {``            ``let d = dist(points[i], points[j]);``            ``if` `(d > right) {``                ``right = d;``            ``}``        ``}``    ``}``    ``while` `(left <= right) {``        ``let mid = (left + right) / 2.0;``        ``let found = ``false``;``        ``// Check if there exist k points inside or on the``        ``// circumference of a circle with radius mid``        ``for` `(let i = 0; i < points.length; i++) {``            ``if` `(count_points_in_circle(points, points[i], mid) >= k) {``                ``found = ``true``;``                ``break``;``            ``}``        ``}``        ``if` `(found) {``            ``right = mid - 1.0;``        ``} ``else` `{``            ``left = mid + 1.0;``        ``}``    ``}``    ``return` `Math.floor(left);``}` `// Example usage``const points = [``    ``[1, 1],``    ``[-1, -1],``    ``[1, -1]``];``const k = 3;``console.log(MinimumRadius(points, k));`

Output

```2
```

Time Complexity: O(n^2 * log(r)) where n is the number of points and r is the maximum distance between any two points.
Space complexity: O(1) as it uses only a constant amount of extra space irrespective of the size of the input.

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.